Nginx完全在60秒后超时

And*_*ich 3 ruby-on-rails nginx server-sent-events ruby-on-rails-4

我在与unix套接字绑定的后端(带有Puma的Rails)上有一个应用服务器,这是nginx config的相关部分

location /live/ {
 proxy_pass http://app; # match the name of upstream directive which is defined above
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_redirect off;
 proxy_set_header Connection 'Upgrade';
 proxy_http_version 1.1;
 chunked_transfer_encoding off;
 send_timeout 300;
 proxy_send_timeout 300;
 keepalive_timeout 7200;
}
Run Code Online (Sandbox Code Playgroud)

SSE事件通过/ live /来,因此我调整了ngix config来处理对该路由的所有请求。

问题是连接完全在60秒后关闭。这是我在响应标题中看到的

Cache-Control:no-cache
Connection:keep-alive
Content-Type:text/event-stream
Date:Mon, 04 Nov 2013 13:41:52 GMT
Server:nginx
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Request-Id:7065a7bc-3450-4fe2-b60c-33dfa8d41951
X-Runtime:0.010852
X-UA-Compatible:chrome=1
X-XSS-Protection:1; mode=block
Run Code Online (Sandbox Code Playgroud)

因此,似乎nginx将初始响应设置为close。为什么keepalive_timeout在这里不起作用。

在nginx error.log中我看到了

2013/11/04 13:42:52 [error] 3689#0: *9 upstream timed out (110: Connection timed out)   while reading upstream, client: ......., server: myapp.com, request: "GET /live/events HTTP/1.1", upstream: "http://unix:/home/ubuntu/myapp/shared/sockets/puma.sock:/live/events", host: "myapp.com"
Run Code Online (Sandbox Code Playgroud)

dav*_*idb 6

我认为您应该检查您的proxy_connect_timeout(默认 60s ;) )和您的proxy_read_timeout(默认 60s),这可能会导致此错误。您可以在此处找到文档:

http://wiki.nginx.org/HttpProxyModule#proxy_connect_timeout


bar*_*olo 5

万一其他人遇到Nginx + Puma + Rails的超时错误的麻烦,Nginx中的以下配置应将超时增加到605秒(10分钟5秒):

server {
    # ... here goes your proxy configuration

    # Avoid 504 HTTP Timeout Errors
    proxy_connect_timeout       605;
    proxy_send_timeout          605;
    proxy_read_timeout          605;
    send_timeout                605;
    keepalive_timeout           605;
}
Run Code Online (Sandbox Code Playgroud)

  • 我在 60 秒有 504 超时。我添加了这些行,但它仍然不起作用。仍然超时 60 秒。有什么帮助吗?我认为美洲狮有什么问题吗? (2认同)