【问题标题】:nginx 502 bad gateway for long running request长时间运行请求的 nginx 502 错误网关
【发布时间】:2016-02-06 17:04:11
【问题描述】:

我有一个需要“几分钟”来处理的请求。当我通过 Django 的内置开发服务器访问它时,这工作正常。

但是,当我从 nginx/uwsgi 访问它时,我得到 502 bad gateway。

我已经尝试增加 nginx.conf 中的超时/keepalive 设置,但没有效果。

这里是相关的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"

【问题讨论】:

标签: django nginx


【解决方案1】:

正如错误所说的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;

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-16
      • 2011-05-14
      • 2019-06-05
      • 2015-08-10
      • 1970-01-01
      • 2019-10-16
      • 2021-11-19
      • 2012-09-25
      相关资源
      最近更新 更多