使用协议升级在nginx反向代理后面运行daphne始终路由到http而不是websocket

Jag*_*u T 6 python django nginx websocket django-channels

我正在尝试在Nginx反向代理服务器后面部署django通道支持的通知应用程序,以仅为websocket通信提供服务,同时让Nginx + uWSGI设置为django应用程序提供服务.

该应用程序在我的本地机器无缝工作时无论是运行python manage.py runserver --noasgi+ daphne -p 8000 myproject.asgi:applicationpython manage.py runserverdaphne内部处理所有的请求.

问题:

所有websocket请求都被路由到http协议类型而不是websocket协议类型,并返回WebSocket connection to 'ws://ip_address/ws/' failed: Error during WebSocket handshake: Unexpected response code: 404

已安装的软件包:

Django==2.0.3
channels==2.0.2
channels-redis==2.1.1
daphne==2.1.0
asgiref==2.3.0
Twisted==17.9.0
aioredis==1.0.0
autobahn==18.4.1
Run Code Online (Sandbox Code Playgroud)

环境:

Ubuntu - 16.04
Nginx - 1.12.0
Run Code Online (Sandbox Code Playgroud)

用于升级请求的Nginx配置:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

# the upstream component nginx needs to connect to websocket requests
upstream websocket {
    server unix:/path/to/my/app/daphne.sock;
}

# configuration of the server
server {

        # the port your site will be served on
        listen      80;

        # the domain name it will serve for
        server_name ip_address;
        charset     utf-8;

       # Sending all non-media requests for websockets to the Daphne server.
        location /ws/ {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }

}
Run Code Online (Sandbox Code Playgroud)

routing.py

from django.conf.urls import url

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from myapp import consumers

application = ProtocolTypeRouter({
    'websocket': AuthMiddlewareStack(
        URLRouter([
            url(r'^ws/$', consumers.MyConsumer),
        ])
    ),
})
Run Code Online (Sandbox Code Playgroud)

达芙妮原木:

None - - [TimeStamp] "GET /ws/" 404 3
None - - [TimeStamp] "GET /ws/" 404 3
None - - [TimeStamp] "GET /ws/" 404 3
Run Code Online (Sandbox Code Playgroud)

如果有任何其他需要帮助,请告诉我.

PS:我在两台服务器中部署了相同的应用程序(两者都具有与上面相同的配置和环境),结果相同.

Jag*_*u T 8

最后我发现罪魁祸首是我公司的防火墙,它在访问应用程序时剥离了升级头http.因此,升级http到之后https,它开始按预期工作.