用于长时间运行请求的nginx 502坏网关

时间:2016-02-04 07:13:58

标签: django nginx

我有一个请求需要几分钟"处理。当我通过Django内置的开发服务器访问它时,这很好用。

然而,当我从nginx / uwsgi访问它时,我得到502坏网关。

我已尝试在nginx.conf中增加超时/保持活动设置但不起作用。

以下是相关的conf设置: -

#keepalive_timeout  0;
client_body_timeout   10;
client_header_timeout 10;
keepalive_timeout     5 5;
send_timeout          10;

和nginx调试日志错误: -

2016/02/03 17:35:33 [notice] 12654#0: nginx/1.4.2
2016/02/03 17:35:33 [notice] 12654#0: built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
2016/02/03 17:35:33 [notice] 12654#0: OS: Linux 2.6.32-358.14.1.el6.x86_64
2016/02/03 17:35:33 [notice] 12654#0: getrlimit(RLIMIT_NOFILE): 1024:4096
2016/02/03 17:35:33 [notice] 12655#0: start worker processes
2016/02/03 17:35:33 [notice] 12655#0: start worker process 12657
2016/02/03 17:35:33 [notice] 12655#0: start worker process 12658
2016/02/03 17:35:33 [notice] 12655#0: start worker process 12659
2016/02/03 17:35:33 [notice] 12655#0: start worker process 12660
2016/02/03 17:36:36 [error] 12658#0: *12 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: xxxxxxxx.in, request: "GET /long/url/with?request=params HTTP/1.1", upstream: "uwsgi://10.176.6.247:8001", host: "xxx.xxx.xxx.xxx"

2 个答案:

答案 0 :(得分:3)

由于错误显示为upstream prematurely closed connection,这意味着它是超时的应用程序服务器。因此,您需要增加应用程序服务器的脚本执行超时设置,即:

#uwsgi.ini
[uwsgi]
harakiri = 200

但是,在增加应用服务器超时后,NGINX代理超时可能会变得太低,然后修改NGIX超时,即:

proxy_connect_timeout 120s;
proxy_read_timeout 200s;

uwsgi_read_timeout 200s;
uwsgi_send_timeout 200s;

但总的来说,正如在评论中已经提到的那样,不建议在Django中使用解析请求的同一进程中执行长时间运行的任务。

答案 1 :(得分:1)

如果我理解的话,问题出在Nginx反向代理和后端服务器之间。您设置的参数是客户端的超时时间。请求,而不是服务器的响应。

我认为(不确定)Nginx配置中的好参数是proxy_read_timeouthttp://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout