Flask上游过早关闭连接

时间:2018-05-30 19:39:06

标签: nginx

请注意,我已提到THIS QUESTION但是这并没有解决问题......

从nginx错误日志中可以看出,我正在向/order-history发送帖子请求。然后,这将运行大约一分钟的SQL查询,但是,连接过早关闭。当应用程序与烧瓶测试服务器一起部署时,不会发生此问题,因为日志指出:)

/var/log/nginx/error.log

2018/05/30 15:34:04 [error] 12294#12294: *9 upstream prematurely closed connection while reading response header from upstream, client: 192.168.96.116, server: alpha2, request: "POST /order-history HTTP/1.1", upstream: "http://unix:/home/hleggio/myproject/myproject.sock:/order-history", host: "alpha2:5000", referrer: "http://alpha2:5000/query-selection"

/etc/nginx//nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    fastcgi_read_timeout 99999;
    proxy_read_timeout 99999;
    # server_tokens off;



    client_max_body_size 20M;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/myproject

server {
    listen 5000;
    server_name alpha2;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/hleggio/myproject/myproject.sock;
        proxy_read_timeout 9999;
        proxy_connect_timeout 9999;
        proxy_request_buffering off;
        proxy_buffering off;
    }
}

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题。这与我的NGINX配置无关。

问题出在我的Gunicorn配置文件中。

在我的Gunicorn配置文件(/etc/systemd/system/myproject.service)中,我将以下内容添加到ExecStart行:

--timeout 600

该文件现在看起来像这样:

[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=harrison
Group=www-data
WorkingDirectory=/home/harrison/myproject
Environment="PATH=/home/harrison/myproject/myprojectenv/bin"
ExecStart=/home/harrison/myproject/myprojectenv/bin/gunicorn --workers 3 --timeout 600 --bind unix:myproject.sock -m 007 wsgi:application

[Install]
WantedBy=multi-user.target