2012-12-26 45 views
15

我有一個奇怪的問題,只有當我使用nginx和獨角獸時纔出現在生產環境中。當我使用沒有nginx的獨角獸時,它不會發生。Nginx,Rails和Oauth。上游過早關閉連接

問題所在。我有一個簡單的oauth認證,允許用戶通過GitHub註冊。在GitHub授權頁面上按下「允許」後,用戶被重定向到回叫路由。然後,他/她得到302 Bad Gateway錯誤。 Nginx的日誌顯示我此錯誤(鍵被替換爲 「...」)

2012年12月26日18時03分08秒[錯誤] 1467#0:* 1上游過早關閉 連接而讀來自上游的響應頭,客戶端: 10.0.2.2,服務器:_,請求:「GET/auth/github/callback?code = & state = ... HTTP/1.1」,upstream: 「http:// unix :/tmp/unicorn.tm.sock:?/認證/ github上/回調代碼= ... &狀態= ...「, 主持人: 」本地主機:3000「

Ther是我的nginx配置。

upstream unicorn { 
    server unix:/tmp/unicorn.tm.sock fail_timeout=0; 
} 

server { 
    listen 80 default deferred; 

    client_max_body_size 4G; 
    server_name _; 

    keepalive_timeout 75s; 

    proxy_connect_timeout 60s; 
    proxy_read_timeout 60s; 


    root /vagrant/public; 

    try_files $uri/index.html $uri.html $uri @app; 

    location @app { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 

    proxy_redirect off; 

    proxy_pass http://unicorn; 

    proxy_buffer_size 16k; 
    proxy_busy_buffers_size 16k; 
    } 

    error_page 500 502 503 504 /500.html; 
} 

所以我的問題是爲什麼會發生這種情況,是否有任何可能的解決辦法呢?

我一直在使用Google,但沒有運氣。

更新

感謝您的評論,我剛纔想設置fail_timeout=30s,它並幫助,但要求大約需要40秒才能完成。但是,無論如何,我會嘗試使用此參數來驗證。

根據建議,我更新了一下我的配置,但仍然得到相同的錯誤。

此外,這是獨角獸錯誤日誌。好像它殺死要求其需要更長的時間超過30秒,但我想從OAuth的網站重定向不可能那麼長......

(github) Request phase initiated. 
(github) Callback phase initiated. 
E, [2012-12-26T19:33:13.058183 #6002] ERROR -- : worker=0 PID:6005 timeout (31s > 30s), killing 
E, [2012-12-26T19:33:13.067011 #6002] ERROR -- : reaped #<Process::Status: pid 6005 SIGKILL (signal 9)> worker=0 
I, [2012-12-26T19:33:13.067198 #6002] INFO -- : worker=0 spawning... 
I, [2012-12-26T19:33:13.068631 #6012] INFO -- : worker=0 spawned pid=6012 
I, [2012-12-26T19:33:13.068726 #6012] INFO -- : Refreshing Gem list 
I, [2012-12-26T19:33:17.140948 #6012] INFO -- : worker=0 ready 

麒麟配置

rails_env = ENV['RAILS_ENV'] || 'production' 

worker_processes 1 

listen "/tmp/unicorn.tm.sock", :backlog => 64 
listen 8080, :tcp_nopush => true 

timeout 30 

pid "/tmp/unicorn.pid" 

stderr_path "/tmp/unicorn.log" 
stdout_path "/tmp/unicorn.log" 

check_client_connection false 
+0

你試過調整keepalive_timeout嗎? –

+1

@EvgeniyRyzhkov,是的,我試圖增加它,但它沒有幫助。日誌中出現相同的錯誤。 – evfwcqcg

+0

有趣的fail_timeout實際上與上游響應時間無關http://wiki.nginx.org/HttpUpstreamModule#server文檔建議使用proxy_connect_timeout和proxy_read_timeout來控制上游響應時間 –

回答

23
ERROR -- : worker=0 PID:6005 timeout (31s > 30s), killing 

不言而喻,你只需要在你的獨角獸配置中設置超時時間超過30秒

試試至少

timeout 60 

http://unicorn.bogomips.org/Unicorn/Configurator.html#method-i-timeout

+1

謝謝你的幫助。在獨角獸的配置中將超時更改爲60秒可以修復錯誤。儘管提供這個請求需要31秒,但問題是關於錯誤和不慢的請求,所以我想會找出問題的其餘部分。 – evfwcqcg

+1

你有沒有設法弄清楚爲什麼它需要這麼長時間?我也偶爾看到類似於我們的導軌/獨角獸/ nginx混合... – gingerlime

+0

我面臨着與Shopify完全相同的問題。 @evfwcqcg - 如果您可以更新有關導致請求緩慢的問題的答案,那將非常棒。 – rohitmishra

相關問題