【问题标题】:How to fix http redirects with Nginx?如何使用 Nginx 修复 http 重定向?
【发布时间】:2017-05-16 21:16:02
【问题描述】:

我有一个网页,其中 http 重定向有点损坏。

目前的行为是这样的:

www.example.com、example.com、http://www.example.comhttp://example.comhttps://www.example.com 都被重定向到https://www.example.com

https://example.com 收到拒绝连接的错误消息。

我希望行为是这样的:

example.com、http://example.comhttps://example.com 重定向到 https://example.com

www.example.com、http://www.example.comhttps://www.example.com 重定向到 https://www.example.com

这是我的 Nginx 配置文件

server {
       listen 80 default_server;
       listen [::]:80 default_server;
       server_name example.com www.example.com;
       return 301 https://$server_name$request_uri;
}

server {
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;

        root /var/www/html;

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

        server_name _;

        location ~ /.well-known {
                allow all;
        }

        location / {
                try_files $uri $uri/ =404;
        }
}

原因是因为我希望这些链接能够正常工作

https://www.ssllabs.com/ssltest/analyze.html?d=example.com

https://www.ssllabs.com/ssltest/analyze.html?d=www.example.com

https://hstspreload.org/?domain=example.com

https://hstspreload.org/?domain=www.example.com

【问题讨论】:

    标签: nginx http-redirect


    【解决方案1】:

    你有两个独立的问题:


    1. 无论最初访问的是哪个特定域,您的请求都会重定向到example.com

    发生这种情况是因为您使用的 $server_name 变量实际上是给定 server 上下文中的静态变量,并且与 $http_host 的关系非常遥远。

    正确的方法是使用 $host 代替(基本上是 $http_host 和一些边角清理)。


    1. 您在尝试联系 https://example.com 而不是 https://www.example.com 时遇到连接问题。

    您的问题中没有足够的信息来查明这个问题的确切根源。

    这可能是 DNS 问题(example.comAAAA 记录设置在未对https 端口进行适当绑定的IP 地址)。

    这可能是证书不匹配的问题:

    • 您的证书是否涵盖example.comwww.example.com?如果没有,那你就不能两者兼得。

    • 如果您有单独的证书,您可能还需要获取单独的 IP 地址,否则可能会因缺少 SNI 而导致大量用户无法访问您的网站。


    值得注意的是,还应该指出的是,在访问您的网站的方式上没有统一的符号通常是一种草率的做法。尤其是如果您对 SEO 有任何顾虑,最佳做法是决定是否要使用 www,并坚持下去。

    【讨论】:

    • 我的证书确实涵盖了 example.com 和 www.example.com。你的权利,为了搜索引擎优化,我希望一切都被重定向到example.com。那么假设我的 DNS 设置正确,我的配置文件应该是什么样子?
    • @user299648,你不是已经说过所有http:// 都被重定向到https://example.com 吗?如果你也想从https 重定向,你应该在https server 上下文中添加类似if ($host != "example.com") {rewrite ^ https://example.com$request_uri? redirect;} 的内容。
    • @user299648,没有理由不工作。另外,如果您希望我们为您提供帮助,恐怕您必须向我们提供更多详细信息,说明究竟是什么不起作用以及确切的症状是什么。
    【解决方案2】:

    你需要这样的东西:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name example.com www.example.com;
        return 301 https://$host$request_uri;
    }
    server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        server_name www.example.com;
    
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;
    
        add_header Strict-Transport-Security "max-age=300; includeSubdomains; preload";
    
        return 301 https://www.example.com$request_uri;
    }
    server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        server_name example.com;
    
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
    
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;
    
        add_header Strict-Transport-Security "max-age=300; includeSubdomains; preload";
    
        location ~ /.well-known {
            allow all;
        }
        location / {
            try_files $uri $uri/ =404;
        }
    }
    

    您的所有请求最终都会路由到https://example.com。 您的 ssl 证书也应该对 https://www.example.com 有效,我注意到您已经说过了。

    【讨论】:

    • @user299648,你试过上面给出的配置吗?应该将所有流量路由到 example.com,就像您提到的那样。请注意,针对预加载添加服务的测试应该只在最大年龄长度上失败,此测试阶段设置为 5 分钟。一旦你确定一切都准备就绪,你就可以把它加起来。
    • @user299648,你能澄清一下吗?如果给定的配置不适合您,那么您的站点肯定有其他问题,因为它是经过实战测试的标准/直接设置。你得到什么错误/结果?例如,您可以在 pastebin 上发布您的实际当前配置吗?我可以 100% 肯定地说,上面给出的 3 服务器块配置是您在 Nginx 上处理 https 重定向的方式。同样,如果不起作用,则缺少有关您的设置的一些信息。 IPTable 重定向可能有效?
    猜你喜欢
    • 2023-03-15
    • 2019-09-30
    • 1970-01-01
    • 2019-10-15
    • 2018-11-27
    • 2021-09-21
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多