【问题标题】:Is this redirect non-www domain to www domain in nginx actually works?这个重定向非 www 域到 nginx 中的 www 域真的有效吗?
【发布时间】:2019-11-22 07:31:37
【问题描述】:

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

更新

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

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
}

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

【问题讨论】:

  • SO上有很多重复的(exampleexampleexample),请在发布新问题之前尝试搜索。
  • @Don'tPanic,我也看过。但我很困惑
  • 那么你需要解释你尝试了什么,并说明你的问题为什么不同。从简单开始 - 删除所有内容并使用这些副本中已知的有效示例之一。
  • 错误很明显 - 您为 www.example.com 配置了冲突的服务器 - 您为 www.example.com 配置了 2 个服务器,为 example.com 配置了 2 个服务器。您正在为自己做这件事——正如我已经说过的那样:从简单的开始——删除所有内容并使用那些重复的已知工作示例之一。它有效。然后慢慢地,一块一块地,添加回你自己的代码,在每一步测试。

标签: nginx redirect


【解决方案1】:

在 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-31
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2018-04-07
    • 2016-11-18
    相关资源
    最近更新 更多