2011-01-06 236 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)或更改此文件夾的所有權。

祝您有愉快的一天。