uwsgi使用多进程导致Nginx报upstream超时问题

一个奇怪的问题,在uwsgi配置使用socket与Nginx通信,且在Django/Flask等使用了多进程时,请求会卡住,具体配置如下:

UWSGI配置:

[uwsgi]
chdir = /home/pi/uwsgiapp
wsgi-file = app.py
callable=app
master = true
processes = 2
vhost = true
socket=127.0.0.1:5050
chmod-socket = 664
http-keepalive=3000
stats=127.0.0.1:1717

Nginx配置:

user www-data;
worker_processes 1;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {


    server {
        listen 80;
        server_name 127.0.0.1;
        charset     utf-8;

        client_max_body_size 75M;   # adjust to taste


        location / {
            include uwsgi_params;
        uwsgi_pass 127.0.0.1:5050;
        proxy_buffering off;
        }
    }

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

app.py内容

import time
from multiprocessing import Process

from flask import Flask, jsonify, request
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False


def hello():
    while True:
        print("ohhhh")
        time.sleep(5)

@app.route('/test/', methods=['GET','POST'])
def getProxiesList():
    p = Process(target=hello)
    p.start()
    return jsonify({'data':'ok'})

启动Nginx和uwsgi后,curl访问http://127.0.0.1/test,uwsgi后台可见已返回请求并每隔5秒打印ohhhh,但curl卡住

curl -v http://127.0.0.1/test/
* Expire in 0 ms for 6 (transfer 0x11fb7c0)
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x11fb7c0)
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /test/ HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.64.0
> Accept: */*
> 

最终触发Nginx超时:[error] 32702#32702: *11 upstream timed out (110: Connection timed out) while reading upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /test HTTP/1.1", upstream: "uwsgi://127.0.0.1:5050", host: "127.0.0.1"

更改uwsgi中process,threads,lazy-apps,close-on-exec均无效。

但以下两种情况多进程可以正常运行

1. uwsgi配置为http服务器,Nginx使用proxy_pass模式转发

2. uwsgi配置enable-threads=true,同时app.py里将多进程替换为多线程模块

目前笔者尚未分析出出现此现象的原因。

上一篇:[error] 11085#0: *1 connect() failed (113: No route to host) while connecting to upstream【nginx错误】


下一篇:Nginx的五种负载均衡策略