I’m running a Flask application with Flask-SocketIO, using NGINX as a reverse proxy and Phusion Passenger to manage the application. My Flask app works fine for HTTP requests, but I’m having trouble with WebSocket connections required by Flask-SocketIO. It appears that Phusion Passenger does not natively support WebSockets, which is causing my WebSocket connections to fail.
WebSocket connections are not working because NGINX can’t reroute the URL to an internal port since Phusion Passenger hasn’t created a listening port for WebSockets.
passenger_wsgi.py
from routes import create_app
application = create_app()
routes/ __ init __.py
from flask import Flask
from .events import socketio
from .routes import main
def create_app():
app = Flask(__name__)
app.config["DEBUG"] = True
app.config["SECRET_KEY"] = "secret"
app.static_folder='templates/static'
app.register_blueprint(main)
socketio.init_app(app)
return app
nginx.conf
location ^~/socketio_test {
root /usr/share/PythonApps/socketio_test/app;
passenger_python /usr/share/PythonApps/socketio_test/env/bin/python;
passenger_base_uri /socketio_test;
passenger_app_root /usr/share/PythonApps/socketio_test/app;
passenger_enabled on;
passenger_app_type wsgi;
passenger_startup_file passenger_wsgi.py;
}
location ^~/socket.io {
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://127.0.0.1:5000/socket.io;
}
index.js
const socket = io({ autoConnect: false, path:'/socket.io/'});
$(document).ready(function () {
socket.connect();
});
I tried:
Updated passenger_wsgi.py to use eventlet for WebSocket support, which opens a port but NGINX can’t use Phusion Passenger this way. If i try to access the website, it will load but not displayed.
Adding configurations like passenger_sticky_sessions on; in NGINX, but it had no effect on resolving the WebSocket issue.
Followed the Flask-SocketIO deployment documentation using uWSGI or their NGINX config with eventlet, but I don’t want to run the application via the console with socketio.run(app) as it isn’t an option for me. However, this method works fine when tested.
I’ve checked the Phusion Passenger documentation, but most of the relevant information seems to be focused on Ruby applications. I haven’t worked with Ruby, and the documentation doesn’t seem to help with my Python-based setup.
AtomFliege is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.