【问题标题】:sendfile() failed (32: Broken pipe) while sending request to upstream nginx 502向上游 nginx 502 发送请求时 sendfile() 失败(32: Broken pipe)
【发布时间】:2016-06-14 00:29:02
【问题描述】:

我正在运行 Django、uwsgi、ngix 服务器。 我的服务器适用于较小的 GET、POST 请求。但是当 POST 大尺寸请求时,nginx 会返回 502: nginx error.log 是:

2016/03/01 13:52:19 [error] 29837#0: *1482 sendfile() failed (32: Broken pipe) while sending request to upstream, client: 175.110.112.36, server: server-ip, request: "POST /me/amptemp/ HTTP/1.1", upstream: "uwsgi://unix:///tmp/uwsgi.sock:", host: "servername"

因此,为了找出真正的问题所在,我在不同的端口上运行了 uwsgi,并检查了同一个请求是否出现任何错误。但是请求成功了。所以,问题出在 nginx 或 unix 套接字配置上。 Ngin-x 配置:

# the upstream component nginx needs to connect to
upstream django {
     server unix:///tmp/uwsgi.sock; # for a file socket
#   server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
# the port your site will be served on
    listen      80;
# the domain name it will serve for
    server_name 52.25.29.179; # substitute your machine's IP address or FQDN
        charset     utf-8;

# max upload size
    client_max_body_size 75M;   # adjust to taste

# Django media
        location /media  {
            alias /home/usman/Projects/trequant/trequant-python/trequant/media;  # your Django project's media files - amend as required
        }

    location /static {
        alias /home/usman/Projects/trequant/trequant-python/trequant/static; # your Django project's static files - amend as required
    }

# Finally, send all non-media requests to the Django server.
    location / {

########    proxy_pass              http://127.0.0.1:8000;
########    proxy_set_header        Host             $host;
########    proxy_set_header        X-Real-IP        $remote_addr;
########    proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;
        uwsgi_pass  django;
        uwsgi_read_timeout 600s;
        uwsgi_send_timeout 600s;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

那么,知道我做错了什么吗?提前谢谢你。

【问题讨论】:

  • 您确定您上传的文件小于 7​​5MB 且请求时间小于 600 秒?
  • 是的,我正在上传 3MB,是的,它不到 600 秒。事实上,nginx 在 30 秒内响应 502 错误。

标签: python django nginx uwsgi


【解决方案1】:

假设在您的 uwsgi.ini 文件中设置 post-buffering = 8192 将解决此问题。我从一个 2.5 岁的答案 here 得到了这个答案,这意味着这个修复不是根本原因。希望对您有所帮助!

【讨论】:

    【解决方案2】:

    另一个解决方法是使用TCP socket instead of a unix socket in your conf files:

    1. 在 uwsgi.ini 中,在 [uwsgi] 部分使用类似 socket = 127.0.0.1:8000 的内容,而不是:

      socket = /tmp/uwsgi.sock chown-socket = nginx:nginx chmod-socket = 664

    2. 在您的 nginx.conf 文件中(顺便说一句,在 Ubuntu 中,我指的是 /etc/nginx/conf.d/nginx.conf,而不是简单地在 /etc/nginx/ 中的那个) 使用uwsgi_pass 127.0.0.1:8000; 而不是include uwsgi_params;

    我已将此作为单独的答案发布,因为任何一个答案都可能有效,我很想看看哪个答案对其他人最有帮助。

    【讨论】:

      【解决方案3】:

      在我的情况下,这似乎是针对会给出 308 重定向的请求。我认为我的 Node 后端在完全接收到 postdata 之前发送了响应。更新客户端以命中新端点(无重定向)可能会永久解决我的问题。看起来很有希望。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-24
        • 2015-05-24
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 2021-07-10
        • 2019-02-09
        • 2017-05-13
        相关资源
        最近更新 更多