• 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里将多进程替换为多线程模块

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

  • 相关阅读:
    骗子网站,X毛都没有,骗我九十九
    xssgame挑战wp
    某xss挑战赛闯关笔记
    committed与urgent的区别
    KahnProcessNetwork的Python实现
    网络层数
    使用matlab用优化后的梯度下降法求解达最小值时参数
    使用matlab画半透明椭圆
    关系代数(Relation Algebra)与SQL语句的对应关系
    one vs all -- 将01分类器用于多类分类问题
  • 原文地址:https://www.cnblogs.com/qjfoidnh/p/14749009.html
Copyright © 2020-2023  润新知