【问题标题】:Nginx too many redirects when redirecting http to https将http重定向到https时,Nginx重定向太多
【发布时间】:2018-09-09 12:22:39
【问题描述】:

我正在尝试将 http 重定向到 https。我将letsencrypt用于ssl证书。我的配置是这样的

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

server {
        listen 443 ssl;
        listen [::]:443 ssl;

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 5m;

        server_name example.com www.example.com;

        root /var/www/landing;

        location /.well-known/ {
                root /var/www/;
        }
}

当我尝试访问 example.com 时,我收到一个浏览器错误,提示重定向太多。 http://example.comhttps://example.com 都出现错误,当我转到 http://www.example.com 时访问服务器块,因为我被重定向到 https://example.com,然后我收到上述错误。

我该如何解决这个问题?

【问题讨论】:

  • 检查您的网站内容,因为它可能有许多硬编码的 http 链接。
  • 我在浏览器的地址栏中输入网址,如果我不进行重定向,它可以工作,所以我认为它与网站内容没有任何关系
  • 如果没有重定向,您的服务器会同时监听端口 80 的 http 请求和端口 443 的 https,没有 http url 从端口 80 重定向到端口 403,当然它“有效”!

标签: http redirect nginx https


【解决方案1】:

在我执行wget -S https://wellcode.com 之后,我认为问题出在 dns 上,所以在 Cloudflare 中我将 SSL 更改为 full,问题就解决了。

说明:

-S 标志将输出标头,从而向您显示重定向。示例:

  HTTP/1.1 301 Moved Permanently
  Server: nginx
  Date: Tue, 05 Jan 2021 12:26:55 GMT
  Content-Type: text/html
  Content-Length: 162
  Connection: close
  Location: https://example.com/foo?bar=baz&dragons=probably
  HTTP/1.1 200 OK
  Server: nginx
  Date: Tue, 05 Jan 2021 12:26:55 GMT
  Content-Type: application/json; charset=utf-8
  Transfer-Encoding: chunked
  Connection: close
  Vary: Accept-Encoding
  X-Powered-By: PHP/7.4.13
  Expires: Tue, 05 Jan 2021 12:26:55 GMT
  Cache-Control: max-age=0
  X-Content-Type-Options: nosniff
  Strict-Transport-Security: max-age=16070400; includeSubDomains

【讨论】:

  • 男人!我只花了3个小时在这上面。非常感谢你偶然发现这一点! :)
猜你喜欢
  • 2011-03-29
  • 2018-01-05
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 2014-10-11
  • 2018-08-18
  • 2017-02-25
  • 1970-01-01
相关资源
最近更新 更多