Nginx 配置域名301重定向跳转

1.例如要将example.com做301重定向到www.example.com

server_name example.com;
rewrite ^/(.*) http://www.example.com/$1 permanent;

2.如果配置其他二级域名都转向到www.example.com

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