uwsgi+nginx+Flask:上游过早关闭

2024-06-08 06:12:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在烧瓶上创建了一个端点,它从数据库查询(remote db)生成一个电子表格,然后将其作为下载文件发送到浏览器中。烧瓶没有任何错误。Uwsgi没有抱怨。

但是当我查看nginx的error.log时,我看到很多

2014/12/10 05:06:24 [error] 14084#0: *239436 upstream prematurely closed connection while reading response header from upstream, client: 34.34.34.34, server: me.com, request: "GET /download/export.csv HTTP/1.1", upstream: "uwsgi://0.0.0.0:5002", host: "me.com", referrer: "https://me.com/download/export.csv"

我部署uwsgi就像

uwsgi --socket 0.0.0.0:5002 --buffer-size=32768 --module server --callab app

我的nginx配置:

server {
     listen 80;
     merge_slashes off;
     server_name me.com www.me.cpm;

     location / { try_files $uri @app; }
       location @app {
          include uwsgi_params;
          uwsgi_pass 0.0.0.0:5002;
          uwsgi_buffer_size 32k;
          uwsgi_buffers 8 32k;
          uwsgi_busy_buffers_size 32k;
     }

}

server {
      listen 443;
      merge_slashes off;
      server_name me.com www.me.com;

    location / { try_files $uri @app; }
       location @app {
          include uwsgi_params;
          uwsgi_pass 0.0.0.0:5002;
          uwsgi_buffer_size 32k;
          uwsgi_buffers 8 32k;
          uwsgi_busy_buffers_size 32k;
       }
}

这是nginx还是uwsgi问题,还是两者都有?


Tags: comappsizeserver烧瓶downloadbuffernginx
2条回答

正如@mahdix所提到的,这个错误可能是由于Nginx使用uwsgi协议发送一个请求,而uwsgi正在该端口上监听http数据包引起的。

在Nginx配置中,有如下内容:

upstream org_app {
    server              10.0.9.79:9597;
}
location / {
    include         uwsgi_params;
    uwsgi_pass      org_app;
}

Nginx将使用uwsgi协议。但如果在uwsgi.ini中有类似的内容(或在命令行中有类似的内容):

http-socket=:9597

uwsgi将http,并且出现问题中提到的错误。见native HTTP support

一个可能的解决办法是:

socket=:9597

在这种情况下,Nginx和uwsgi将通过TCP连接使用uwsgi协议相互通信。

旁注:如果Nginx和uwsgi在同一个节点上,Unix套接字将比TCP快。见using Unix sockets instead of ports

将nginx.conf更改为包含

sendfile        on;
client_max_body_size 20M;
keepalive_timeout  0;

完整示例请参见self-answeruwsgi upstart on amazon linux

相关问题 更多 >

    热门问题