nginx301重定向配置
第一种方法判断域名是否是www.xxx.com如果不是所有域名跳转到www.xxx.com
server {
server_name www.xxx.com xxx.com;
if ($host != 'www.xxx.com' ) {
rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
}
}
第二种方法判断域名是否是xxx.com如果是跳转到www.xxx.com(推荐使用)
server {
if ($host = 'xxx.com'){
rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
}
}
检查配置
/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
测试成功, 重启nginx,输入命令:
/usr/local/nginx/sbin/nginx -s reload