Kaka32
Kaka32

Reputation: 3

Flask-SocketIO, Nginx Proxy Manager and Cloudflare setup

I'm trying to setup a little web application. It's a small flask application, run by uwsgi. uwsgi expose the port 8090 for the application, while the app itself runs on 5080. Here's the content of the ini file :

[uwsgi]
wsgi-file = main.py
master = true
processes = 5
callable = app
http-websockets = true
http = :8090
gevent = 1000
socket = website.sock
vacuum = true
die-on-term = true

logto = /home/website/log-access.log

And I want to use Flask-SocketIO for some realime data.

When I tested it on my local machine, everything works like a charm.

Now, I want to make it work online.

I've got a cloudflare protection in front of the server. The cloudflare setup is, A record with proxied traffic to my server's IP. The SSL/TLS section is set to "Full".

On my server, I have running Nginx Proxy Manager with docker. It is setup as this : Proxy host --> mydomain.com, http://ipaddress:8090 Block Common Exploits and Websockets Support checked. SSL certificate, with Force SSL and HTTP/2 Support checked. I added this to the Advanced tab :

location /socket.io/ {
    proxy_pass http://127.0.0.1:8090/socket.io/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

I tried to replace the proxy_pass URL, with the public IP, domain name, and app's port (5080).

As for my flask setup, nothing much.

main.py

import logging
logging.basicConfig(format='%(message)s', level=logging.INFO)
from core.app import app, socketio
from core.models import db, Users
from core.routes import routes, socket_routes
from flask_login import LoginManager

with app.app_context():
    db.init_app(app)
    db.create_all()
app.register_blueprint(routes)
app.register_blueprint(socket_routes)

login_manager = LoginManager()
login_manager.login_view = 'routes.login'
login_manager.init_app(app)

@login_manager.user_loader
def load_user(id_user):
    return db.session.get(Users, int(id_user))


if __name__ == '__main__':
    socketio.run(app, host="127.0.0.1", debug=True, port=5080)

My app.py (called by a main.py file) is this :

from flask import Flask, request
import logging
logging.basicConfig(format='%(message)s', level=logging.INFO)
from core import config

from flask_socketio import SocketIO

core_config = CoreConfig.LoadConf()

app = Flask(__name__)
app.config['SECRET_KEY'] = ''
app.config['SERVER_NAME'] = config['SERVER_NAME']

socketio = SocketIO(app)

Then I have a blueprint especially for the Flask-SocketIO In which I have pretty much :

from .app import socketio


@socketio.on('connect')
def connect():
    print('joined socketio')

The rest are just other socket.io functions

The whole server has ufw enabled. Because of docker I used this script : https://github.com/chaifeng/ufw-docker it works fine. I oppened the port for the app (8090) and nginx 443.

Now, when I got to the route where the websocket is active, I get an error :

It is a "Bad gateway Error, 502" from cloudflare.

I also tried to add :

socketio = SocketIO(app, cors_allowed_origins = '*')

But same errors.

I don't really know what to do, so thank you for your time.

Upvotes: 0

Views: 99

Answers (0)

Related Questions