Nginx 实现 www 域名 301 跳转

1.将 example.com 定向到 www.example.com

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

2.将 www.example.com 定向到 example.com

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