Running Flask-SocketIO on WebFaction through mod_wsgi

元气小坏坏 提交于 2019-12-12 03:36:19

问题


I have deployed a Flask application on Webfaction using mod_wsgi. My application is pretty simple but does implement Flask-SocketIO which is giving me troubles. My code works fine on my localhost but now that it is running on my Webfaction server the client is unable to connect to my socket. I'm not quite sure where my problems are coming from – I'm assuming I haven't set up my apache config file properly but this may not be true.

On the client side I receive a 400 (Bad Request) error on both GET & POST calls to the websocket. Intermittently I see a warning notifying me the socket closed before a connection was established. I also get an error occasionally stating that there was an error during WebSocket handshake.

My apache httpd.conf file is as follows:

ServerRoot "/home/< user >/webapps/< my_app >/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule unixd_module      modules/mod_unixd.so

Listen 18161
KeepAlive On
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

WSGIDaemonProcess < my_app > processes=2 threads=12 python-path=/home/< user >/webapps/< my_app >:/home/< user >/webapps/< my_app >/lib/python2.7
WSGIProcessGroup < my_app >
WSGIRestrictEmbedded On
WSGILazyInitialization On

WSGIScriptAlias / /home/< user >/webapps/< my_app >/wsgi.py

I've seen some posts about proxy_module & proxy_http_module but I'm not quite sure if I need that and if so how exactly to configure it. Any guidance on this issue would be much appreciated!


回答1:


That stuff about proxy_module and proxy_http_module is for using Apache as a front-end proxy for a back-end websockets app. It's not applicable to what you're trying to do.

mod_wsgi is not really suitable for serving websocket applications. Don't use it - instead, use one of the deployment options recommended in the Deployment section of the Flask-SocketIO docs.

For example, to use the first recommended option (ie, the embedded server) on WebFaction, first install the eventlet module. I'm assuming you've already installed Flask and Flask-SocketIO at this point.

Next, install a 'custom websockets app listening on port' application via the WebFaction control panel. Make a note of the app's assigned port, then assign the app to a website in the control panel. You do not need to open the app's port.

Next, configure your Flask-SocketIO app to run on the port assigned to your websockets app. The simplest way to do this is to pass the port to socketio.run, eg:

if __name__ == '__main__':
    socketio.run(app, port=12345)

In your javascript, connect like this:

var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);

Finally, just run the app:

python2.7 whatever.py

At that point, your app will be up and running on whatever domain you assigned to the website you set up a few steps back.

When you load the site, JS will sort out the connection info from document.domain and location.port. If you open up your browser's dev tools (Chrome inspector or whatever) you'll see the websockets connections being made to ws://yourdomain.com/ .

Hope that helps!



来源:https://stackoverflow.com/questions/36168552/running-flask-socketio-on-webfaction-through-mod-wsgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!