简体   繁体   中英

Getting 502 bad gateway and 400 bad request with flask_socketio + flask + gunicorn + nginx

So I have been setting up a website with flask + flask_socketio + eventlet + gunicorn + nginx. I have looked at the documentation for flask_socketio for this configuration and it is supported. Though after using the website I noticed that while flask would work perfectly, flask_socketio would not work at all. Here are the relevant configuration and error codes. I would like to note that some flask_socketio bits post requests would go through with a 200 OK.
This is on ubuntu 18.04
Nginx:

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

events {
    worker_connections 65535;
    multi_accept on;
}

http {

    ##
    # Basic Settings
    ##
    charset utf-8;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;
    log_not_found off;
    client_max_body_size 16M;
    # 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_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
    ##
server {
    listen 80;
    server_name http://compoundingly.com;

    location / {
        proxy_pass http://0.0.0.0:5000;
    proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /static {
        alias /home/shorya/compoundingly_website/compoundingly_code/static;
        expires 30d;
    }
    location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://0.0.0.0:5000/socket.io;
    }
}

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



Gunicorn service

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

[Service]
User=root
Group=www-data
WorkingDirectory=/home/shorya/compoundingly_website/compoundingly_code
Environment="PATH=/home/shorya/compoundingly_website/compoundingly_code/compoundingly_venv/bin"
ExecStart=/home/shorya/compoundingly_website/compoundingly_code/compoundingly_venv/bin/gunicorn -k eventlet --workers 3 --bind unix:compoundingly_code.sock -m 007 wsgi:app
Restart=on-failure
[Install]
WantedBy=multi-user.target

wsgi.py

from app import app

if __name__ == "__main__":
    app.run()

pip3 freeze

click==7.1.2
dnspython==1.16.0
eventlet==0.26.1
Flask==1.1.2
Flask-SocketIO==4.3.1
greenlet==0.4.16
gunicorn==19.7.1
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
monotonic==1.5
pkg-resources==0.0.0
python-engineio==3.13.1
python-socketio==4.6.0
six==1.15.0
Werkzeug==1.0.1

failed request

URL: http://compoundingly.com/socket.io/?EIO=3&transport=polling&t=NFGwVcP&sid=22ad9ddeaffe4eeb8525927aca86806b
Status: 400 BAD REQUEST
Source: Network
Address: 192.241.149.99:80
Initiator: 
index.js:83

good request

URL: http://compoundingly.com/socket.io/?EIO=3&transport=polling&t=NFGwVtd
Status: 200 OK
Source: Network
Address: 192.241.149.99:80
Initiator: 
index.js:83

502 bad gateway request

URL: http://compoundingly.com/socket.io/?EIO=3&transport=polling&t=NFGwdji&sid=69f16b45a1aa474294cdeb2452f0e71a
Status: 502 Bad Gateway
Source: Network
Address: 192.241.149.99:80
Initiator: 
index.js:83

Gunicorn error log


Aug 08 22:09:16 sm01 gunicorn[1956]: [2020-08-08 22:09:16 -0400] [5964] [INFO] Booting worker with pid: 5964
Aug 08 22:09:47 sm01 gunicorn[1956]: [2020-08-08 22:09:47 -0400] [1956] [CRITICAL] WORKER TIMEOUT (pid:5962)
Aug 08 22:09:47 sm01 gunicorn[1956]: [2020-08-08 22:09:47 -0400] [5962] [INFO] Worker exiting (pid: 5962)
Aug 08 22:09:47 sm01 gunicorn[1956]: [2020-08-08 22:09:47 -0400] [5977] [INFO] Booting worker with pid: 5977
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [1956] [CRITICAL] WORKER TIMEOUT (pid:5963)
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [1956] [CRITICAL] WORKER TIMEOUT (pid:5964)
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [5963] [INFO] Worker exiting (pid: 5963)
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [5964] [INFO] Worker exiting (pid: 5964)
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [5981] [INFO] Booting worker with pid: 5981
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [5982] [INFO] Booting worker with pid: 5982

For people who are facing this same problem there are a few things you can do to make the system work after you have done this: I changed my wsgi to:

from app import *

if __name__ == "__main__":
    #app.run()
    app = Flask(__name__)#,template_folder="./dist/app/templates"
    app.config['SECRET_KEY'] = 'secret!'
    socketio = SocketIO(app,async_mode="eventlet")
    socketio.run(app,host="0.0.0.0",port=5000,debug=True)

added --bind 0.0.0.0:5000 to the gunicorn command.
I also downloaded a newer version of gunicorn but not the newest.
19.7 does not work
20.0.3 works
20.0.4 does not work

See the documentation for how to run Gunicorn with eventlet. In particular, you are using multiple workers, which is not supported.

Example supported configuration from the docs:

gunicorn --worker-class eventlet -w 1 module:app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM