首页 > 解决方案 > 客户端发送 ws://[host]:[port]/path 到服务器,我的服务器是 Nginx+uwsgi+Django,但是当我启动 get to server 时总是遇到 404 错误

问题描述

Websocket 不能被 Nginx+Uwsgi 接受到我的 Django 我用 Django 写了一些 url_patterns 例如

http://[host]:[port]/events

我需要处理一个 websocket

<ws://[host]:[port]/alarm>

Nginx 听 10001 和 uwsgi 听 10002 我已经添加了一些到我的 nginx.conf

server
    {
        listen 10001;
        root  /xxx;

        location /alarm {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:10002;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_redirect off;
        }
        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:10002;
            uwsgi_read_timeout 60;
        }
    ......

但它仍然不起作用,当我看到浏览器时,我看到了这个

Status Code:404 Not Found

我选择uwsgi来实现我的websocket,在我的测试中我写了这个

ws.create_connection("ws://127.0.0.1:10001/alarm")

我用它来启动我的应用程序

uwsgi --init uwsgi.ini --http_websockets

我在 uwsgi 输出中看到了这个

GET /alarm => generated 77 bytes in 6 msecs (HTTP/1.1 404) 6 headers in 207 bytes (1 switches on core 0)

web_client.py 在下面给了我错误

Handshaken 404

我的 uwsgi.ini 是这个

WEBSOCKET_FACTORY_CLASS="dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory"
http = :8008
socket = :10002

在我的 django 中,我写了这个

application = ProtocolTypeRouter({
    'websocket':AuthMiddlewareStack(
        URLRouter(
            alarm.routing.channel_routing
        )
    ),

那么我的错在哪里?

标签: djangonginxwebsocketuwsgi

解决方案


推荐阅读