uwsgi + nginx + flask: upstream prematurely closed

后端 未结 8 1955
暗喜
暗喜 2020-12-16 13:28

I created an endpoint on my flask which generates a spreadsheet from a database query (remote db) and then sends it as a download in the browser. Flask doesn\'t throw any er

8条回答
  •  鱼传尺愫
    2020-12-16 14:05

    As mentioned by @mahdix, the error can be caused by Nginx sending a request with the uwsgi protocol while uwsgi is listening on that port for http packets.

    When in the Nginx config you have something like:

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

    Nginx will use the uwsgi protocol. But if in uwsgi.ini you have something like (or its equivalent in the command line):

    http-socket=:9597
    

    uwsgi will speak http, and the error mentioned in the question appears. See native HTTP support.

    A possible fix is to have instead:

    socket=:9597
    

    In which case Nginx and uwsgi will communicate with each other using the uwsgi protocol over a TCP connection.

    Side note: if Nginx and uwsgi are in the same node, a Unix socket will be faster than TCP. See using Unix sockets instead of ports.

提交回复
热议问题