正确配置Flask SocketIO

时间:2015-06-19 11:04:18

标签: python nginx gunicorn flask-socketio

我一直在关注本教程,尝试使用Flask SocketIOnginx运行gunicorn

nginx的

server {
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_redirect off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /socket.io {
        proxy_pass http://localhost:8000/socket.io;
        proxy_redirect off;
        proxy_buffering off;

        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";

    }

}

gunicorn_config.py

bind = '127.0.0.1:8000'
workers = 2
worker_class = 'socketio.sgunicorn.GeventSocketIOWorker'

Supervisor我用我的应用程序调用:

[program:gunicorn-couponmonk]
directory = ~/couponmonk_project
command =~/venv/py2.7/bin/python    ~/venv/py2.7/bin/gunicorn -c ~/venv/py2.7/lib/python2.7/site-packages/gunicorn/gunicorn_config.py __init__.py 
stdout_logfile = /var/log/gunicorn/couponmonk-std.log
stderr_logfile = /var/log/gunicorn/couponmonk-err.log
user = mint

使用此配置,我的应用程序(不使用Flask SocketIO)工作正常。

我只是对如何使用socketIO感到困惑。如果我转到地址http://localhost:8000/socket.io,我会收到回复Internal Server Error

示例HTML/Javascripthttps://github.com/miguelgrinberg/Flask-SocketIO/blob/master/example/templates/index.html)包含以下行:

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

我有两个问题:

1)此变量应指向哪个地址?

2)我的配置(nginxgunicorn)是否正确?

对不起,如果这个问题很愚蠢。我只是对这一切应该如何运作感到困惑。

感谢您的帮助。

**更新**

当我尝试访问http://localhost/socket.io时,这是nginx error.log文件的输出。

2015/06/20 14:05:08 [debug] 1917#0: *1 http cleanup add: 00000000009F2170
2015/06/20 14:05:08 [debug] 1917#0: *1 get rr peer, try: 1
2015/06/20 14:05:08 [debug] 1917#0: *1 socket 12
2015/06/20 14:05:08 [debug] 1917#0: *1 epoll add connection: fd:12 ev:80000005
2015/06/20 14:05:08 [debug] 1917#0: *1 connect to 127.0.0.1:8000, fd:12 #2
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream connect: -2
2015/06/20 14:05:08 [debug] 1917#0: *1 posix_memalign: 0000000000A14170:128 @16
2015/06/20 14:05:08 [debug] 1917#0: *1 event timer add: 12: 60000:1434773168437
2015/06/20 14:05:08 [debug] 1917#0: *1 http finalize request: -4, "/socket.io?" a:1, c:2
2015/06/20 14:05:08 [debug] 1917#0: *1 http request count:2 blk:0
2015/06/20 14:05:08 [debug] 1917#0: *1 post event 0000000000A295E0
2015/06/20 14:05:08 [debug] 1917#0: *1 post event 0000000000A29648
2015/06/20 14:05:08 [debug] 1917#0: *1 delete posted event 0000000000A29648
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream request: "/socket.io?"
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream send request handler
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream send request
2015/06/20 14:05:08 [debug] 1917#0: *1 chain writer buf fl:1 s:439
2015/06/20 14:05:08 [debug] 1917#0: *1 chain writer in: 00000000009F21A8
2015/06/20 14:05:08 [debug] 1917#0: *1 writev: 439
2015/06/20 14:05:08 [debug] 1917#0: *1 chain writer out: 0000000000000000
2015/06/20 14:05:08 [debug] 1917#0: *1 event timer del: 12: 1434773168437
2015/06/20 14:05:08 [debug] 1917#0: *1 event timer add: 12: 60000:1434773168437
2015/06/20 14:05:08 [debug] 1917#0: *1 delete posted event 0000000000A295E0
2015/06/20 14:05:08 [debug] 1917#0: *1 http run request: "/socket.io?"
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream check client, write event:1, "/socket.io"
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream recv(): -1 (11: Resource temporarily unavailable)
2015/06/20 14:05:08 [debug] 1917#0: *1 post event 0000000000A15E38
2015/06/20 14:05:08 [debug] 1917#0: *1 post event 0000000000A29648
2015/06/20 14:05:08 [debug] 1917#0: *1 delete posted event 0000000000A29648
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream request: "/socket.io?"
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream dummy handler
2015/06/20 14:05:08 [debug] 1917#0: *1 delete posted event 0000000000A15E38
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream request: "/socket.io?"
2015/06/20 14:05:08 [debug] 1917#0: *1 http upstream process header
2015/06/20 14:05:08 [debug] 1917#0: *1 malloc: 00000000009E88A0:4096
2015/06/20 14:05:08 [debug] 1917#0: *1 recv: fd:12 244 of 4096
2015/06/20 14:05:08 [debug] 1917#0: *1 http proxy status 500 "500 Internal Server Error"
2015/06/20 14:05:08 [debug] 1917#0: *1 http proxy header: "Connection: close"
2015/06/20 14:05:08 [debug] 1917#0: *1 http proxy header: "Content-Type: text/html"
2015/06/20 14:05:08 [debug] 1917#0: *1 http proxy header: "Content-Length: 141"
2015/06/20 14:05:08 [debug] 1917#0: *1 http proxy header done
2015/06/20 14:05:08 [debug] 1917#0: *1 xslt filter header
2015/06/20 14:05:08 [debug] 1917#0: *1 HTTP/1.1 500 Internal Server Error
Server: nginx/1.4.6 (Ubuntu)
Date: Sat, 20 Jun 2015 04:05:08 GMT
Content-Type: text/html
Content-Length: 141
Connection: keep-alive

不确定这有多大帮助,但我不知道在哪里寻找额外的信息。

我也很好奇为什么我可以访问它:http://localhost/socket.io123abc

并且仍然获得Internal Server Error而不是Not Found错误?

我还根据以下supervisord.conf回答更新了我的Miguel's文件:

supervisord.conf

[program:gunicorn-couponmonk]
directory = /home/giri/couponmonk_project
command = /home/giri/venv/py2.7/bin/python /home/giri/venv/py2.7/bin/gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker __init__:app 
stdout_logfile = /var/log/gunicorn/couponmonk-std.log
stderr_logfile = /var/log/gunicorn/couponmonk-err.log
user = mint

/var/log/gunicorn/couponmonk-err.log

2015-06-20 14:30:11 [3821] [ERROR] Connection in use: ('127.0.0.1', 8000)
2015-06-20 14:30:11 [3821] [ERROR] Retrying in 1 second.
2015-06-20 14:30:12 [3821] [ERROR] Connection in use: ('127.0.0.1', 8000)
2015-06-20 14:30:12 [3821] [ERROR] Retrying in 1 second.
2015-06-20 14:30:13 [3821] [ERROR] Connection in use: ('127.0.0.1', 8000)
2015-06-20 14:30:13 [3821] [ERROR] Retrying in 1 second.

此列表会持续一段时间..

3 个答案:

答案 0 :(得分:2)

我建议您在没有nginx和gunicorn的情况下使Flask-SocketIO正常工作。一旦你可以通过本机gevent服务器工作,你就可以转到真正的设置。

关于你的问题:

  

1)这个变量应该指向什么地址?

您的连接声明是正确的。 Socket.IO将获取主机,端口和命名空间,并自行构建连接URL,包括/socket.io组件。您无需在连接中指定。

  

2)我的配置(nginx,gunicorn)是否正确?

我认为nginx配置是正确的。您似乎直接从我的文档中复制了它,并且我已经验证它可以正常工作。

枪声配置我不确定,你没有展示你的项目。我使用的命令,你应该在你的supervisor配置中有这个:

gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app

其中module是应用程序的主要模块,app是Flask应用程序实例的名称。你绝对应该使用一个工人,在使用SocketIO时不要使用两个工人。

答案 1 :(得分:1)

首先,我提到我根本不建议使用SocketIO。它在WebSockets上添加了一些有用的功能,但它使得多个工作人员(水平扩展)的真正负载平衡变得不可能,除非您让客户端对各个工作人员发粘或使用Redis之类的东西来共享它们之间的状态信息。我建议看看:

https://github.com/youen/gevent-websocket

Native WebSockets更简单,更容易,可跨多个前端工作人员进行扩展,实现真正的负载平衡。实现聊天室逻辑所需的代码非常少。

话虽如此,这里是您问题的答案:

  

1)这个变量应该指向什么地址?

您的命名空间将是""。 (根据Flask-SocketIO的作者Miguel的说法,Javascript客户端会自动插入" socket.io"部分。)

因此,您的io.connect网址应为" http://[hostname]/"。

  

2)我的配置(nginx,gunicorn)是否正确?

Ngnix配置似乎是正确的(虽然见下一节)。如果您决定使用WebSockets,则可以考虑添加proxy_read_timeout 3600;。否则,除非你有一个繁琐的协议,否则每分钟Nginx会删除一次WebSocket(默认值)。 (另外,根据Miguel的说法,SocketIO有处理此问题的心跳。)

Gunicorn配置不正确。使用SocketIO,您有几个选择:

  1. 在gunicorn中设置workers = 1,以便每个SocketIO客户端与同一个工作进程通信。
  2. 将您的nginx配置更改为使用ip-hash命令,这将导致客户端按客户端IP地址分配给工作人员。
  3. 使用Redis或其他一些数据库允许每个工作人员共享状态信息(虽然不清楚是否有人为Gevent SocketIO工作)。
  4. 这些选择是强制的,因为SocketIO使用有状态的设置机制,当您尝试水平扩展时会中断。有关详细信息,请参阅此问题:https://github.com/abourget/gevent-socketio/issues/112

    此处还有一个指向SocketIO文档的链接:http://socket.io/docs/using-multiple-nodes/

    如果您收到Internal Server Error,则可能会在某处记录异常。尝试追捕它并将其添加到您的问题中。

    请注意,您无法通过在浏览器的地址栏中点击该网址对其进行测试 - 默认情况下,浏览器不会说出正确的WebSocket协议,并且对您没有任何帮助。必须使用Javascript API设置WebSocket连接。

    此外,尝试使用端口号命中该URL会绕过nginx - 这可能不是您想要做的。 Nginx通常会在80/443收听并将请求转发给localhost:8000(这被称为"反向代理"设置)。

答案 2 :(得分:1)

对于正在阅读此旧帖子的任何人,gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app命令已经过时,正如github flask-socketio issue之一中提到的Miguel。