AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 839309
Accepted
THpubs
THpubs
Asked: 2017-03-20 17:31:03 +0800 CST2017-03-20 17:31:03 +0800 CST 2017-03-20 17:31:03 +0800 CST

Nginx 配置在尝试将 www 重定向到非 www 和 https 时不断抛出重定向循环

  • 772

我正在尝试将所有对我的域的请求重新定向www到非 www url,并将所有请求同时重定向到https。这是我的 nginx 配置:

server {
    listen 80;
    server_name example.com www.example.com;

    location '/.well-known' {
      default_type "text/plain";
      root        /home/letsencrypt;
      allow all;
    }

    return 301 https://example.com$request_uri;
}

server {
  listen              443 ssl http2;
  server_name         www.example.com;

  include snippets/ssl-example.com.conf;
  include snippets/ssl-params.conf;


  location '/.well-known' {
        default_type "text/plain";
        root        /home/letsencrypt;
        allow all;
  }

  return 301 https://example.com$request_uri;
}

server {
  listen  443 ssl http2;
  server_name  example.com;

  access_log            /var/log/nginx/slmun.access.log;
  error_log             /var/log/nginx/slmun.error.log;

  include snippets/ssl-example.com.conf;
  include snippets/ssl-params.conf;

  location '/.well-known' {
    default_type "text/plain";
    root        /home/letsencrypt;
    allow all;
  }


  location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forward-Proto http;
    proxy_set_header X-Nginx-Proxy true;

    proxy_redirect off;
  }
}

问题是,我在这里得到了一个重定向循环。浏览器说example.com redirected you too many times.。知道发生了什么吗?

https://slmun.org的卷曲:

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

这是访问日志条目:

162.158.165.25 - - [20/Mar/2017:07:50:53 +0530] "GET / HTTP/1.1" 301 185 "-" "curl/7.47.0"

奇怪的 https 也被重定向。

卷曲slmun.org(这是真正的域):

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

这是访问日志条目:

162.158.165.25 - - [20/Mar/2017:07:51:49 +0530] "GET / HTTP/1.1" 301 185 "-" "curl/7.47.0"

www 的也有相同的结果。

nginx
  • 1 1 个回答
  • 1557 Views

1 个回答

  • Voted
  1. Best Answer
    Tim
    2017-03-20T18:58:19+08:002017-03-20T18:58:19+08:00

    您没有在问题中提到您正在使用 CloudFlare。我相信问题出在 CloudFlare,无论是缓存结果还是 PageRule。在进行此诊断时,清除您的 CloudFlare 缓存并禁用任何页面规则。

    下一个选项是您的代理应用程序发送重定向。查看 Nginx 和代理应用的访问日志确认。

    如果您对此不满意,请发布您的 CloudFlare 配置和 Nginx 配置文件的屏幕截图,以及代理配置的日志。

    这是来自 Firefox 的带有 Live HTTP Headers 插件的请求/响应标头。我可以确认一个循环重定向。

    https://slmun.org/
    
    GET / HTTP/1.1
    Host: slmun.org
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101     Firefox/51.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate, br
    DNT: 1
    Connection: keep-alive
    Upgrade-Insecure-Requests: 1
    

    回复

    HTTP/2.0 301 Moved Permanently
    Date: Mon, 20 Mar 2017 02:56:33 GMT
    Content-Type: text/html
    Set-Cookie: __cfduid=d8f2af34526d1d1beae191bf7de70440a1489978593; expires=Tue, 20-Mar-18 02:56:33 GMT; path=/; domain=.slmun.org; HttpOnly
    Location: https://slmun.org/
    Server: cloudflare-nginx
    cf-ray: 342566200f8418f0-AKL
    X-Firefox-Spdy: h2
    
    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve