[英]Running daphne behind nginx reverse proxy with protocol upgrade always routes to http instead of websocket
I am trying to deploy django channels powered notification application behind Nginx reverse proxy server to only serve the websocket communications, while having Nginx + uWSGI setup to serve the django application. 我正在尝试在Nginx反向代理服务器后面部署django通道支持的通知应用程序,以仅为websocket通信提供服务,同时让Nginx + uWSGI设置为django应用程序提供服务。
The application works in my local machine seamlessly when run with either python manage.py runserver --noasgi
+ daphne -p 8000 myproject.asgi:application
or python manage.py runserver
with daphne
handling all the requests internally. 当使用
python manage.py runserver --noasgi
+ daphne -p 8000 myproject.asgi:application
或python manage.py runserver
与daphne
在内部处理所有请求时,应用程序无缝地在我的本地机器上运行。
Problem: 问题:
All the websocket requests are routed to http
protocol type instead of websocket
protocol type and it returns WebSocket connection to 'ws://ip_address/ws/' failed: Error during WebSocket handshake: Unexpected response code: 404
所有websocket请求都被路由到
http
协议类型而不是websocket
协议类型,它返回WebSocket connection to 'ws://ip_address/ws/' failed: Error during WebSocket handshake: Unexpected response code: 404
Packages Installed: 已安装的软件包:
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
Environment: 环境:
Ubuntu - 16.04
Nginx - 1.12.0
Nginx Configuration for upgrading the request: 用于升级请求的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;
}
}
routing.py 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),
])
),
})
Daphne Logs: 达芙妮原木:
None - - [TimeStamp] "GET /ws/" 404 3
None - - [TimeStamp] "GET /ws/" 404 3
None - - [TimeStamp] "GET /ws/" 404 3
Kindly let me know if anything in addition is required to help. 如果有任何其他需要帮助,请告诉我。
PS: I have deployed the same application in two servers(both having the same configuration and environment as above) and the result was same. PS:我在两台服务器中部署了相同的应用程序(两者都具有与上面相同的配置和环境),结果相同。
Finally I found that the culprit was my company's firewall, which was stripping the upgrade headers when application was accessed over http
. 最后我发现罪魁祸首是我公司的防火墙,当通过
http
访问应用程序时,它正在剥离升级头。 So after upgrading http
to https
, it started working as expected. 因此,在将
http
升级到https
,它开始按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.