Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1491

Flask-SocketIO, Nginx Proxy Manager and Cloudflare setup

$
0
0

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.pymaster = trueprocesses = 5callable = apphttp-websockets = truehttp = :8090gevent = 1000socket = website.sockvacuum = truedie-on-term = truelogto = /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:8090Block 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 logginglogging.basicConfig(format='%(message)s', level=logging.INFO)from core.app import app, socketiofrom core.models import db, Usersfrom core.routes import routes, socket_routesfrom flask_login import LoginManagerwith 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_loaderdef 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, requestimport logginglogging.basicConfig(format='%(message)s', level=logging.INFO)from core import configfrom flask_socketio import SocketIOcore_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-SocketIOIn 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.


Viewing all articles
Browse latest Browse all 1491

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>