Nginx,Rails和Oauth。上游过早关闭连接 [英] Nginx, Rails and Oauth. upstream prematurely closed connection
问题描述
我有一个奇怪的问题,只有在我使用nginx和unicorn时才出现在生产环境中。当我使用没有nginx的独角兽时,它不会发生。
I have a strange problem that appears in production environment only when I use nginx and unicorn. When I use unicorn without nginx, it doesn't happen.
问题所在。我有一个简单的oauth身份验证,允许用户通过GitHub注册。在GitHub的授权页面上按允许后,用户被重定向到回叫路由。然后,他/她得到 302 Bad Gateway
错误。
Nginx日志显示我的错误(键被替换为...)
The problem. I have a simple oauth authentication which allows users to signup through GitHub. After pressing "allow" on GitHub's authorization page, the user is being redirected to callback route. And then, he/she gets 302 Bad Gateway
error.
Nginx log shows me this error (keys are replaced by "...")
2012/12/26 18: 03:08 [错误] 1467#0:* 1上游过早关闭
连接,同时从上游读取响应头,客户端:
10.0.2.2,服务器:_,请求:GET / auth / github / callback?code =& state = ...
HTTP / 1.1,上游:
http:// unix:/tmp/unicorn.tm.sock:/ auth / github / callback?code = ...& state = ...,
主持人:localhost:3000
2012/12/26 18:03:08 [error] 1467#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 10.0.2.2, server: _, request: "GET /auth/github/callback?code=&state=... HTTP/1.1", upstream: "http://unix:/tmp/unicorn.tm.sock:/auth/github/callback?code=...&state=...", host: "localhost:3000"
Ther是我的nginx config。
Ther is my nginx config.
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;
}
所以我的问题是为什么会发生这种情况并且有任何可能的修复方法?
So my question is why does this happen and is there any possible fix for it?
我已经谷歌搜索了一段时间,但没有运气。
I have been googling it for a while, but without luck.
更新
感谢您的评论,我刚刚尝试设置 fail_timeout = 30s
并且它确实有帮助,但是请求大约需要40秒才能完成。但是,无论如何,我会尝试使用这些参数来使用。
Thanks for the comment, I have just tried to set fail_timeout=30s
and it does help, however request takes about 40s to complete. But thanks anyway, I'll try to expirement with this parameters.
我根据建议稍微更新了我的配置,但仍然得到同样的错误。
I updated my config a bit, according to suggestions, but still, get the same error.
此外,这是独角兽错误日志。好像它会杀死超过30秒的请求,但我想从oauth网站重定向可能不会那么长......
Also, this is unicorn error log. Seems like it kills request which takes longer than 30s but I guess redirection from oauth site cannot possibly be that long...
(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
unicorn config
unicorn config
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
推荐答案
ERROR -- : worker=0 PID:6005 timeout (31s > 30s), killing
毫无疑问,你需要在你的unicorn配置中设置超时到30以上
It goes without saying that you just need setup timeout to more then 30 in you unicorn config
至少尝试
timeout 60
http://unicorn.bogomips.org/Unicorn/ Configurator.html#method-i-timeout
这篇关于Nginx,Rails和Oauth。上游过早关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!