当uWSGI处理请求时间较长时,Nginx会超时。

50

我有一个 Python Django 应用,使用 Nginx + uWSGI 进行部署。

在我的 nginx.conf 文件中,我有以下内容:

location / {
    include uwsgi_params;
    uwsgi_pass   127.0.0.1:9001;
    uwsgi_read_timeout 1800;
    uwsgi_send_timeout 300;
    client_header_timeout 300;
    proxy_read_timeout 300;
    index  index.html index.htm;
}

但对于在uWSGI上长时间运行约1分钟才能完成的请求,我在Nginx错误日志中得到了超时错误,如下所示:

2013/04/22 12:35:56 [error] 2709#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: xx.xx.xx.xx, server: , request: "GET /entity/datasenders/ HTTP/1.1", upstream: "uwsgi://127.0.0.1:9001", host: "xxx.xx.xx.x"

我已经将头部超时和uWSGI发送/读取超时设置为5分钟,请问有什么办法可以解决这个问题吗?

4个回答

82

解决问题的配置是:

location / {
    include uwsgi_params;
    uwsgi_pass   127.0.0.1:9001;
    uwsgi_read_timeout 300;
    index  index.html index.htm;
}

问题中上述配置对我们不起作用的原因是,不幸的是在我们的机器上有多个路径都有 nginx.conf 文件。我们使用了错误路径下的配置文件。

要正确找出Nginx正在从哪个路径获取配置,请运行以下命令:

nginx -V  # V is caps

这将有一个--conf-path=[],它将告诉您确切地从哪里获取配置。

我最近发现上述的nginx -V没有提供正确的信息。为了方便其他人参考,我会保留上述内容。


2
那个数字代表什么?秒数吗?如果我们将其设置为像2000这样的大数字,会有问题吗? - senaps

5

通过更改以下Nginx配置解决问题

proxy_connect_timeout 300;
proxy_read_timeout    300;


client_body_timeout   300;
client_header_timeout 300;
keepalive_timeout     300;

关于UWSGI的设置

http-timeout = 300 // or 'socket-timeout = 300' depending on uwsgi setting

0
除了“uwsgi_read_timeout”答案之外,您还应该检查nginx uwsgi缓存目录的所有权是否正确。所有权必须设置为与运行nginx进程相同的用户... 在我的情况下,我不得不这样做。
grep '^user' /etc/nginx/nginx.conf
ls -lah /var/cache/nginx/uwsgi_temp
for f in $( find /var/cache/nginx/uwsgi_temp ); do ls -lah $f; done

这些文件是否属于同一用户?如果不是,您可以关闭nginx并删除所有缓存文件,确保/var/cache/nginx/uwsgi_temp上有正确的所有者,然后重新启动。也许您还可以尝试递归更改所有权,但我没有测试过这种方法。
# store the user
THEUSER=$(grep '^user' /etc/nginx/nginx.conf | sed 's/.* //; s/;.*//' )

清除缓存和重启方法

/etc/init.d/nginx stop
rm -rf /var/cache/nginx/uwsgi_temp/* 
chown $THEUSER:$THEUSER /var/cache/nginx/uwsgi_temp
/etc/init.d/nginx start

递归更改文件属主的方法

chown -R $THEUSER:$THEGROUP /var/cache/nginx/uwsgi_temp/
# not sure if you have to restart nginx here... 

0

查看uwsgi错误日志并理解问题的本质对我很有帮助。问题根本不是与Nginx配置相关。我的电子邮件主机已更改,代码在调用发送电子邮件代码时抛出了错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接