通过mod_wsgi在WebFaction上运行Flask-SocketIO [英] Running Flask-SocketIO on WebFaction through mod_wsgi

查看:228
本文介绍了通过mod_wsgi在WebFaction上运行Flask-SocketIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用mod_wsgi在Webfaction上部署了Flask应用程序.我的应用程序很简单,但是实现了Flask-SocketIO,这给我带来了麻烦.我的代码在我的本地主机上运行良好,但是现在它在Webfaction服务器上运行,客户端无法连接到我的套接字.我不太确定我的问题来自哪里–我假设我没有正确设置我的apache配置文件,但这可能不正确.

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.

在客户端,我在GET&上均收到400(错误请求)错误. POST到websocket的调用.我间歇性地看到一条警告,通知我在建立连接之前已关闭套接字.我偶尔也会收到一条错误消息,指出在WebSocket握手过程中发生了错误.

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.

我的Apache httpd.conf文件如下:

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

我看过一些关于proxy_module& proxy_http_module,但我不确定是否需要,以及如何准确配置它.在这个问题上的任何指导将不胜感激!

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!

推荐答案

关于proxy_moduleproxy_http_module的内容是用于将Apache用作后端websockets应用程序的前端代理.它不适用于您要执行的操作.

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并不真正适合于服务websocket应用程序.不要使用它-而是使用部署"部分中建议的部署选项之一Flask-SocketIO文档.

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.

例如,要在WebFaction上使用第一个推荐的选项(即嵌入式服务器),请首先安装eventlet模块.我假设您已经安装了Flask和Flask-SocketIO.

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.

接下来,通过WebFaction控制面板安装一个自定义websockets应用程序监听端口"应用程序.记下该应用程序的分配端口,然后在控制面板中将该应用程序分配到网站.您无需打开应用程序的端口.

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.

接下来,将Flask-SocketIO应用程序配置为在分配给websockets应用程序的端口上运行.最简单的方法是将端口传递到socketio.run,例如:

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)

在您的JavaScript中,像这样连接:

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.

加载网站时,JS将从document.domainlocation.port中整理连接信息.如果打开浏览器的开发工具(Chrome检查器或其他工具),您将看到与ws://yourdomain.com/建立的websockets连接.

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/ .

希望有帮助!

这篇关于通过mod_wsgi在WebFaction上运行Flask-SocketIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆