2011-10-22 36 views
2

我的堆棧看起來像這樣nginx - > thin - > rails。在我的Rails應用程序我在applicaton_controller.rb:使用nginx在rails中強制ssl登錄用戶

if (!Rails.env.development?) 
    before_filter :force_ssl 
    end 

    # Force logged in users to use SSL 
    def force_ssl 
    if current_user && request.protocol != "https://" 
     redirect_to :protocol => "https://" 
    end 
    end 

的問題是所有請求看起來像HTTP,因爲nginx的處理SSL並轉發到薄,導致無限重定向循環。在這種情況下,爲登錄用戶設置ssl的正確方法是什麼?

回答

5

可以使用proxy_set_header指令設置自定義標題告訴你的後端,請求來自安全前端。

例子:

for 80: 
proxy_set_header   X-SSL  0; 

for 443: 
proxy_set_header   X-SSL  1; 

或全球

proxy_set_header X-Forwarded-Proto $scheme; 
2

爲什麼不爲所有用戶使用https?

Google,Github和其他人可能已經開始這樣做了。

你可以直接在你的nginx配置中做到這一點。

server { 
    listen 80; 

    location/{ 
    rewrite ^(.*) https://$host$1 permanent; 
    } 

這將所有80端口的流量重定向到HTTPS和端口443

然後,只需申報正常的服務器端口443

server { 
    listen 443; 
    // whatever 
} 
+2

1.損害緩存,2需要SSL協議(並非無足輕重,如果你考慮到頁面上使用的各個領域:礦山,谷歌分析,谷歌CDN,我的CDN,臉譜,微博等),3.不標準的做法。我不知道谷歌是否這樣(http://www.google.com仍然適用於我) – jhchen