2011-01-06 48 views
25

因此,我已经看过了我能找到的每个示例配置,但每次尝试查看需要ssl的页面时,我都会在重定向循环中结束。我运行nginx/0.8.53和乘客3.0.2。Nginx配置导致无尽的重定向循环

这里的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; 
    } 
} 

让我知道,如果有任何额外的信息,我可以给你帮助诊断问题。

+0

属于http://serverfault.com – 2011-01-06 17:13:36

+3

布莱恩,这个问题是否解决? – Joseph 2011-05-02 13:49:08

回答

5

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

我遇到了这个头问题之前,它没有导致重定向,但改变这个头修复它给我。

+0

它也解决了我的问题。谢谢;) – Luis 2011-02-23 16:27:43

+0

也一样。 X_FORWARDED_PROTO对特定的应用程序没有任何帮助,而X-Forwarded-Proto的效果很好。 nginx代理到后端的Passenger Standalone rails应用程序。 – furinkan 2011-08-10 01:14:38

30

这是你的线在这里:

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摆脱了我的无限循环问题。

+3

我在一个Rails应用程序中使用`config.ssl = true`,第一个版本的nginx的ssl配置也结束了一个无限循环。更改配置使ssl声明在单独的行上解决了我的问题。谢谢!!! – Adam 2012-01-05 14:40:25

4

既然你已经在SSL和非SSL部分

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

哪里是blog..com服务器部分重写语句中找到?这可能是问题的根源吗?

1

如果有人遇到这种情况,我试图通过同一个服务器块来配置http和https,但只添加了“listen 443”指令,认为“这一行是默认和隐含的”意味着它也会听80,但它没有。取消注释“listen 80”行,以便两条听线都存在,纠正了无限循环。不知道为什么它甚至会得到重定向,但它确实如此。

5

我发现,这是该行

proxy_set_header Host $http_host; 

应该由使用“$ HTTP_HOST你传递的‘不变的请求头’改为

proxy_set_header Host $host; 

According to the nginx documentation

3

我对我的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; 
    } 
1

对于那些谁正在寻找desperatly为什么他们owncloud不断,尽管有一个良好的配置文件进行重定向循环,我已经找到了,为什么它不工作。

我的配置: 的nginx + PHP-FPM + MySQL在一个新的CentOS 6.5

安装PHP-FPM和nginx的,放在/ var默认权限时,/ lib中/ PHP /会话/是根:阿帕奇

php-fpm通过nginx存储php会话在这里,如果nginx没有授权写入它会失败,以保持任何登录会话,导致无限循环。

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

祝您有愉快的一天。