【问题标题】:Nginx configuration leads to endless redirect loopNginx 配置导致无限重定向循环
【发布时间】:2011-06-04 17:31:05
【问题描述】:

因此,我查看了可以找到的每个示例配置,但每次尝试查看需要 ssl 的页面时,我都会陷入重定向循环。我正在运行 nginx/0.8.53 和乘客 3.0.2。

这是 ssl 配置

server  {
  listen 443 default ssl;
  server_name <redacted>.com www.<redacted>.com;
  root /home/app/<redacted>/public;
  passenger_enabled on;
  rails_env production;  
  ssl_certificate      /home/app/ssl/<redacted>.com.pem;
  ssl_certificate_key  /home/app/ssl/<redacted>.key;

  proxy_set_header  X-Real-IP  $remote_addr;
  proxy_set_header  X_FORWARDED_PROTO https;
  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header  Host $http_host;
  proxy_set_header  X-Url-Scheme $scheme;
  proxy_redirect    off;
  proxy_max_temp_file_size 0;

  location /blog {
    rewrite ^/blog(/.*)?$ http://blog.<redacted>.com/$1 permanent;
  }

  location ~* \.(js|css|jpg|jpeg|gif|png)$ {
    if (-f $request_filename) {
      expires      max;
      break;
    }
  }

  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   html;
  }
}

这是非 SSL 配置

server  {
  listen 80;
  server_name <redacted>.com www.<redacted>.com;
  root /home/app/<redacted>/public;
  passenger_enabled on;
  rails_env production;  

  location /blog {
    rewrite ^/blog(/.*)?$ http://blog.<redacted>.com/$1 permanent;
  }

  location ~* \.(js|css|jpg|jpeg|gif|png)$ {
    if (-f $request_filename) {
      expires      max;
      break;
    }
  }

  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   html;
  }
}

如果我可以提供任何其他信息来帮助诊断问题,请告诉我。

【问题讨论】:

    标签: ssl nginx ruby-on-rails-3 passenger


    【解决方案1】:

    这是你的台词:

      listen 443 default ssl;
    

    改成:

    listen 443;
    ssl on;
    

    我称之为旧样式。 另外,还有

                  proxy_set_header X_FORWARDED_PROTO https;
                  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header  Host $http_host;
                  proxy_set_header  X-Url-Scheme $scheme;
                  proxy_redirect    off;
                  proxy_max_temp_file_size 0;
    

    为我做了诀窍。我现在看到我缺少您拥有的真实 IP 线路,但到目前为止,这解决了我的 ssl_requirement 和 ssl_enforcer 的无限循环问题。

    【讨论】:

    • 我在 Rails 应用程序中使用 config.ssl = true 和 nginx 的 ssl 配置的第一个版本,结果也陷入了无限循环。更改配置以将 ssl 声明放在单独的行上解决了我的问题。谢谢!!!
    • ssl 开启;当我切换 config.ssl = true 时,在新行上为我修复了无限循环
    【解决方案2】:

    我玩弄了一堆这样的答案,但对我没有任何帮助。然后我意识到,由于我使用 Cloudflare,问题可能不在服务器上,而是在 Cloudflare 上。瞧,当我将 SSL 设置为 Full (Strict) 时,一切正常!

    【讨论】:

    • 这个问题快把我逼疯了!我已经使用 Let's Encrypt 设置了 SSL,所以我什至没有考虑检查 Cloudflare 的 SSL 设置。感谢分享!
    • 非常感谢,我差点把头发都拔光了!
    【解决方案3】:

    我发现是这一行

     proxy_set_header  Host $http_host;
    

    应该改成

     proxy_set_header  Host $host;
    

    According to the nginx documentation 使用 '$http_host 您正在传递“未更改的请求标头”。

    【讨论】:

      【解决方案4】:

      您是否尝试过使用“X-Forwarded-Proto”而不是 X_FORWARDED_PROTO?

      我之前遇到过此标头的问题,它没有导致重定向,但更改此标头为我解决了问题。

      【讨论】:

      • 我也一样。 X_FORWARDED_PROTO 对特定应用程序没有任何作用,而 X-Forwarded-Proto 效果很好。 nginx 代理到后端的Passenger Standalone rails 应用程序。
      【解决方案5】:

      因为您在 ssl 和非 ssl 部分都找到了 rewrite 语句

      location /blog {
        rewrite ^/blog(/.*)?$ http://blog.<redacted>.com/$1 permanent;
      }
      

      blog..com 的服务器部分在哪里?这可能是问题的根源吗?

      【讨论】:

        【解决方案6】:

        我的 symfony2 应用程序也遇到了类似的问题,尽管原因不同:我在 nginx 配置中当然需要 fastcgi_param HTTPS on; 时设置了 fastcgi_param HTTPS off;

            location ~ ^/(app|app_dev|config)\.php(/|$) {
                    satisfy any;
                    allow all;
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_split_path_info ^(.+\.php)(/.*)$;
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_param HTTPS on;
            }
        

        【讨论】:

        • @Sahil 不要在 cmets 中感谢,而是投赞成票。很高兴它有帮助:)
        【解决方案7】:

        万一其他人偶然发现这一点,我试图通过同一个服务器 {} 块同时配置 http 和 https,但只添加了“listen 443”指令,相信“此行是默认和隐含的”意味着它也会在 80 上收听,但它没有。取消对“listen 80”行的注释,以便两个监听行都存在,从而纠正了无限循环。不知道为什么它甚至会获得重定向,但确实如此。

        【讨论】:

          【解决方案8】:

          对于那些拼命寻找为什么他们自己的云尽管有一个很好的配置文件但仍然进行重定向循环的人,我已经找到了它为什么不起作用的原因。

          我的配置: nginx + php-fpm + mysql 在新的 centos 6.5 上

          安装php-fpm和nginx时,/var/lib/php/session/默认权限为root:apache

          php-fpm 通过 nginx 在这里存储 php session,如果 nginx 没有权限写入它就惨不忍睹,无法保持任何登录 session,导致死循环。

          所以只需在 apache 组中添加 nginx (usermod -a -G apache nginx) 或更改此文件夹的所有权。

          祝你有美好的一天。

          【讨论】:

            【解决方案9】:

            X_FORWARDED_PROTO 在您的文件中可能会导致错误,在我的情况下确实如此。 X-Forwarded-Proto 是正确的,而 hiphens 比大写或小写字母更重要。

            您可以通过遵守约定来避免这些问题;)

            参见此处:Custom HTTP headers : naming conventions 和此处:http://www.ietf.org/rfc/rfc2047.txt

            【讨论】:

              猜你喜欢
              • 2012-03-05
              • 2016-02-02
              • 2015-11-28
              • 2013-08-28
              • 2019-08-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-04-19
              相关资源
              最近更新 更多