【问题标题】:Nginx timeouts when uWSGI takes long to process request当 uWSGI 处理请求需要很长时间时 Nginx 超时
【发布时间】:2013-04-15 00:43:39
【问题描述】:

我有用于 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 : ,请求:“GET /entity/datasenders/HTTP/1.1”,上游:“uwsgi://127.0.0.1:9001”,主机:“xxx.xx.xx.x”

我已经将标头超时和 uWSGI 发送/读取超时设置为 5 分钟,有人可以告诉我如何解决这个问题吗?

【问题讨论】:

    标签: nginx configuration uwsgi connection-timeout


    【解决方案1】:

    解决问题的配置是:

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

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

    要正确找出您的 nginx 从运行中获取配置的路径:

    nginx -V  # V is caps
    

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

    我最近发现上述nginx -V 没有提供正确的信息。我将保留以上内容,以防其他人发现它有用。

    【讨论】:

    • 那个数字代表什么?秒?如果我们将它设置为像 2000 这样的大数字,会不会有问题?
    【解决方案2】:

    通过更改以下 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
    

    【讨论】:

      【解决方案3】:

      除了“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 上并重新启动。也许你也可以只做一个递归 chown,我没有测试这种方法。

      # 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 方法

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-15
          • 2018-11-10
          • 1970-01-01
          • 1970-01-01
          • 2020-11-10
          • 2022-10-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多