无法使用Nginx + uwsgi + django同时处理websocket和xhr请求

问题描述 投票:1回答:1

我正在尝试使用Nginx + uwsgi + django建立一个网站,其中一个任务需要websocket。该系统在django服务器(python manage.py runserver)中运行良好。但是,当我使用Nginx + uwsgi在AWS上创建时,出错了。虽然我修改了Nginx + uwsgi的设置,但我发现服务器无法同时处理websocket和xhr请求

具体问题如下:

1)如果我使用uwsgi --socket :8001 --module project.wsgi --http-websockets启动uwsgi,可以建立websocket,但django的“request.websocket”中的消息始终为none,uwsgi日志显示warning: async call without async mode

2)接下来,我尝试使用uwsgi --socket :8001 --module project.wsgi --http-websockets --async 10 --ugreen在uwsgi中使用异步模式。在这种情况下,websocket运行良好但其他xhr请求被阻止,并且在websocket结束之前服务器无法处理。例如,如果一个客户端使用websocket运行任务,则其他客户端甚至无法登录Web。

你能帮我解决这个问题吗?

提前致谢!

django nginx websocket uwsgi
1个回答
0
投票

您可以根据需要将以下代码段添加到位于sites-available的nginx配置文件中。

这使您可以访问url ws://wsbackend/wspath/上的websockets

location /ws/ {
        proxy_pass http://unix:/tmp/lcs_daphne.sock;
        proxy_http_version 1.1;

        proxy_read_timeout 86400;
        proxy_redirect     off;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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_set_header X-Forwarded-Host $server_name;
    }

这是切换协议,它将http请求升级到ws请求。

有关this的更多信息,请参阅implementation of websockets using nginx文档

© www.soinside.com 2019 - 2024. All rights reserved.