-1

我的域名有以下 nginx 服务器块example.com。我想将非 www 重定向到 www 以进行 SEO。

更新

根据这个答案,我使用了以下服务器块。但是当我测试它时,我得到了以下

nginx: [warn] conflicting server name "www.example.com" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

所以,我怀疑它是否正确,以及它是否真的将非 www 重定向到 www,请。

/etc/nginx/sites-available/example.com

server {
    server_name www.example.com;
    rewrite ^(.*) https://www.example.com$1 permanent;
}

server {
        root /var/www/abc-company-website/public;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name example.com; 

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }


        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }


        location ~ /\.ht {
                deny all;
        }

        #Cache-Control
        location ~* \.(?:ico|ttf|png|svg|jpg|jpeg|js)$
        {
            expires 7d;
            add_header Pragma public;
            add_header Cache-Control "public";
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}


server {

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
        server_name example.com www.example.com;
    return 404; # managed by Certbot
}

请问如何将上述服务器块更改为重定向?

4

1 回答 1

0

在 nginx 的最佳实践中,不使用 if(如果您将它用于 $host 则更有原因),最好使用具有不同 server_name 的服务器括号。

server {
    listen 80; 
    server_name example.org www.example.org;
    return 301 https://www.example.org$request_uri;
}

这会将 HTTP www 和 HTTP 非 www 发送到 HTTPS www

如果您有非 www 的证书,请设置服务器括号并重定向到 www:

server{
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    return 301 https://www.example.com$request_uri;
} 

最后,您可以在 https www.example.com 括号中做任何您想做的事情:

server {

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    server_name www.example.com; 

     # Do whatever you want to do here to get to your application

 }

最好阅读 nginx 的文档和最佳实践,并尝试进行干净的配置,以便下一个可以第一眼理解它:D

如果您有任何问题,请提出。(看起来你不明白评论中给出的重复项,所以我决定为你的情况一一解释)

于 2019-11-22T12:56:57.420 回答