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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật