gevent-socketio nginx uwsgi不能在开发服务器上一起工作

时间:2013-05-23 16:05:46

标签: django nginx uwsgi gevent gevent-socketio

我正在运行一个使用gevent-socketio的django项目。

由于某些原因,在我的开发服务器上,我的所有websockets请求都返回一条101 pending消息,socketio将开始循环通过导致pending状态的所有其他协议。

我的uwsgi日志中的错误是:

2013/05/23 16:09:08 [error] 14485#0: *85 upstream timed out (110: Connection timed out) while reading upstream, client: x.x.x.x, server: dev.proj.co, request: "GET /socket.io/1/xhr-polling/116404981619?t=1369325348489 HTTP/1.1", upstream: "http://127.0.0.1:4042/socket.io/1/xhr-polling/116404981619?t=1369325348489", host: "dev.proj.co", referrer: "http://dev.proj.co/map/bycon/"

在当地,我没有这个问题。我使用python run.py

启动服务器 在我的本地环境

run.py

#!/usr/bin/env python
import os
import sys

from gevent import monkey
monkey.patch_all()

import django.core.handlers.wsgi
from socketio.server import SocketIOServer

import os

PORT = 8000

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
application = django.core.handlers.wsgi.WSGIHandler()
PROJECT_DIR            = os.path.realpath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_DIR)
sys.path.insert(0, os.path.join(PROJECT_DIR, "chat"))

if __name__ == '__main__':
    SocketIOServer(('', PORT), application, resource="socket.io").serve_forever()

在发生错误的开发服务器上,我有以下设置:

nginx.conf

worker_processes  auto;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    upstream django {
        server 127.0.0.1:4042;
    }


    server {
        listen       80;
        server_name dev.proj.co;
        charset utf-8;

        access_log  /var/log/nginx/myproj_dev.access.log;
        error_log  /var/log/nginx/myproj_dev.error.log;

        location /media/ {
            alias /var/www/dev/myproj/releases/myproj_public/media/;
            error_page 404 = /404;
            expires 30d;
        }

        location /static/ {
            alias /var/www/dev/myproj/releases/myproj_public/static/;
            error_page 404 = /404;
            expires 30d;
        }
        location / {
            proxy_pass http://127.0.0.1:4042;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}

uwsgi_dev.ini

[uwsgi]

if-env = PROJECT_HOME
print = Your path is %(_)/current
chdir = %(_)/current
touch-reload = %(_)/current/myproj/uwsgi_dev.ini
daemonize = %(_)/myproj_uwsgi/myproj.log
endif =

if-env = VIRTUAL_ENV
print = Your virtualenv is %(_)
virtualenv = %(_)
endif = 

gevent = 100
processes = 4
module = myproj.wsgi_dev
env = DJANGO_SETTINGS_MODULE=myproj.settings.dev
master = True
vacuum = True
max-requests = 5000
logdate = True

# newrelic requirements
enable-threads = True
single-interpreter = True

wsgi_dev.py

import os

from gevent import monkey
monkey.patch_all()

from socketio.server import SocketIOServer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproj.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

PORT = 4042
SocketIOServer(("127.0.0.1", PORT), application, \
        resource="socket.io").serve_forever()

其他信息:

I'm using uwsgi 1.9.6, gevent-websocket 0.3.6, greenlet 0.4.0 and the dev version of gevent(-e git://github.com/surfly/gevent.git@e2f51c1358e88b60e45d1daf8ee263da64066576#egg=gevent-dev) and gevent-socketetio (-e git://github.com/abourget/gevent-socketio.git@aeece7038b0052ddf6b4228857e4d7a67a6242f2#egg=gevent_socketio-dev)

[root@li476-12 ~]# nginx -v
nginx version: nginx/1.4.1

3 个答案:

答案 0 :(得分:1)

对于websockets支持,您需要最新版本的nginx(1.4.x)。如果您使用旧版本,则无法启动websocket频道

答案 1 :(得分:0)

当您将WSGI django文件作为模块运行时,SocketIOServer永远不会运行,因此__name__不是__main __

除此之外逻辑是错误的,因为SocketIOServer将接管uWSGI阻止yorur django app。

我建议你使用uWSGI的--attach-daemon从单独的脚本运行SocketIOServer

(显然将uWSGi放在不同的端口上)

答案 2 :(得分:0)

禁用UWSGI中的线程。

您有“Global Interpretator Lock”问题

enable-threads = True< - 我认为这里有问题,请尝试False