將不帶www的域名重定向到帶www的域名,規則如下:
if($host='56zx.com'){
rewrite ^/(.*)$ http://www.56zx.com/$1 permanent;
}
原因是nginx語法檢測特別嚴格,if和後面括號以及變量等號這些元素都要有空格,所以正確的規則是:
if ( $host = '56zx.com' ){
rewrite ^/(.*)$ http://www.56zx.com/$1 permanent;
}
如果用^代替空格,規則如下:
if^(^$host^=^'56zx.com'^){
rewrite ^/(.*)$ http://www.56zx.com/$1 permanent;
}