在server段域名下设置:

if ( $host != 'www.xxxx.com' ) {        
     rewrite ^/(.*)$ http://www.xxxx.com/$1 permanent; 
}

permanent – 返回永久重定向的HTTP状态301

这里要注意的是,nginx语法检测特别严格,if和后面括号以及变量等号这些元素都要有空格。

如果没有空格的话会报[emerg] unknown directive “if($host!=”这样的错误。


检查配置是否正确

/usr/local/nginx/sbin/nginx -t

提示:

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

说明配置文件无错误

/usr/local/nginx/sbin/nginx -s reload

重启nginx即可。