django - 长时间运行请求的 nginx 502 错误网关

标签 django nginx

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

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

我尝试增加 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"

最佳答案

正如错误所述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 在解析请求的同一进程中执行长时间运行的任务。

关于django - 长时间运行请求的 nginx 502 错误网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194733/

相关文章:

docker - Nginx 反向代理到主机中的应用程序

nginx - nginx 可以在不使用分块编码的情况下发送动态压缩的内容吗?

ubuntu - 如何在 Digital Ocean Ubuntu 服务器上将非 www 重定向到 www?

python - 导入错误: No module named 'parse'

python - python 的 timthumb?

django - Heroku:我的 Django 应用程序出现应用程序错误 H14: "No Web Processes Running"

ssl - 未捕获( promise )DOMException : Failed to register a ServiceWorker: An SSL certificate error occurred when fetching the script

django - 潜在的 docker 权限问题导致枕头说它没有安装,尽管它是

python - 如何解决 FOREIGN KEY 约束失败

django - 我不知道为什么 Nginx 服务器不工作