1

我正在使用django-websocket-redis来设置 websocket。还使用 nginx 和 docker。我遵循了用法中提到的内容,尝试从客户端访问服务器时出错。

这是我的 nginx.conf

  location /api {
    include             /etc/nginx/uwsgi_params;
    uwsgi_param         HTTP_HOST {{ 'DOMAIN_NAME' | env }};
    uwsgi_pass          uwsgi://{{ upstream }};
    uwsgi_read_timeout  600;
}

 location /ws/ {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    include         /etc/nginx/uwsgi_params;
    uwsgi_param     HTTP_HOST {{ 'DOMAIN_NAME' | env }};
    uwsgi_pass      unix:///tmp/app_ws.sock;
}

这里是我的 uwsgi:

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir               = $(SRC)/
# Django's wsgi file
module              = config.wsgi
# the virtualenv (full path)
# home                = $(DEPLOY_ROOT)/app/
# process-related settings
# master
master              = true

# set up app load optimizations
workers             = 4         # maximum number of workers
cheaper             = 2         # minimum number of workers at all times
cheaper-initial     = 2         # num. of workers spawned at startup
cheaper-step        = 1         # num. of workers spawned when algo decides more are needed
cheaper-algo        = spare     # (spare2) faster spawning, slower disposing
cheaper-busyness-verbose = true

# align header size with ELB
buffer-size         = 16384

# use network socket
socket              = :xxxx

# clear environment on exit
vacuum              = true

# reload django app on file change (debug only)
if-env              = PYTHON_AUTORELOAD
python-autoreload   = %(_)
end-if              =

pythonpath          = $(SRC)/
base                = $(SRC)/

这是我用于 websocket 的 uwsgi:

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir               = $(SRC)/
# Django's wsgi file
module              = config.wsgi_websocket
# the virtualenv (full path)
# home                = $(DEPLOY_ROOT)/app/

http-websockets = true
gevent          = 1000

# process-related settings
# master
master          = true

# set up app load optimizations
workers         = 4         # maximum number of workers
cheap           = 2         # minimum number of workers at all times
cheaper-initial = 2         # num. of workers spawned at startup
cheaper-step    = 1         # num. of workers spawned when algo decides more are needed
cheaper-algo    = spare     # (spare2) faster spawning, slower disposing

# align header size with ELB
buffer-size     = 16384
# the socket (use the full path to be safe
socket          = /tmp/app_ws.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 667
# clear environment on exit
vacuum          = true

# reload django app on file change (debug only)
if-env = PYTHON_AUTORELOAD
python-autoreload = %(_)
end-if =


pythonpath          = $(SRC)/
base                = $(SRC)/

这是我得到的错误。

app_1         | *** running gevent loop engine [addr:0x493be0] ***
app_1         | Python auto-reloader enabled
app_1         | Traceback (most recent call last):
app_1         | Traceback (most recent call last):
app_1         |   File "gevent.libev.corecext.pyx", line 490, in gevent.libev.corecext.loop.io (src/gevent/libev/gevent.corecext.c:6698)
app_1         |   File "gevent.libev.corecext.pyx", line 490, in gevent.libev.corecext.loop.io (src/gevent/libev/gevent.corecext.c:6698)
app_1         |   File "gevent.libev.corecext.pyx", line 850, in gevent.libev.corecext.io.__init__ (src/gevent/libev/gevent.corecext.c:11105)
app_1         |   File "gevent.libev.corecext.pyx", line 850, in gevent.libev.corecext.io.__init__ (src/gevent/libev/gevent.corecext.c:11105)
app_1         | ValueError: fd must be non-negative: -1
app_1         | ValueError: fd must be non-negative: -1

如果没有 websocket,我的应用程序可以正常工作,这意味着我可以点击 /api 下的 url。

4

0 回答 0