我有一个需要“几分钟”来处理的请求。当我通过 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;
Run Code Online (Sandbox Code Playgroud)
和 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"
Run Code Online (Sandbox Code Playgroud)
正如错误所说upstream prematurely closed connection,这意味着它是应用程序服务器超时。因此你需要增加你的应用服务器的脚本执行超时设置,即:
#uwsgi.ini
[uwsgi]
harakiri = 200
Run Code Online (Sandbox Code Playgroud)
但是,在增加应用程序服务器超时后,NGINX 代理超时可能会变得太低,然后也修改 NGIX 超时,即:
proxy_connect_timeout 120s;
proxy_read_timeout 200s;
Run Code Online (Sandbox Code Playgroud)
或者
uwsgi_read_timeout 200s;
uwsgi_send_timeout 200s;
Run Code Online (Sandbox Code Playgroud)
但总的来说,正如评论中已经提到的,Django 不建议在解析请求的同一进程中执行长时间运行的任务。
| 归档时间: |
|
| 查看次数: |
7468 次 |
| 最近记录: |