我有一个带有通道的Django项目,在managy.py runserver
上可以正常工作,但在Nginx和daphne上却很迷恋。
该项目的常规HTTP部分由Gunicorn提供,并且运行良好。
当我尝试访问已部署的页面时,浏览器控制台抛出WebSocket connection to 'ws://MY_URL/ws/chat/controller/' failed: Error during WebSocket handshake: Unexpected response code: 400
,当然,与WebSocket相关的功能不起作用。
由于该站点在测试服务器上运行正常,因此我认为问题出在Nginx和Daphne的部署部分。这是我认为相关的所有文件:
#nginx.conf
#added this block
upstream channels-backend {
server localhost:9001;
}
server {
listen 80;
server_name MY_URL;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /PROJECT_DIR;
}
location / {
include proxy_params;
proxy_pass http://unix:PROJECT_DIR/demoserver.sock;
}
location /ws/ {
proxy_pass http://channels-backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
proxy_redirect off;
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;
}
}
#MY_PROJECT-daphne.service
[Unit]
Description=Demoserver Daphne Service
After=network.target
[Service]
Type=simple
User=MY_USER
WorkingDirectory=PROJECR_DIR
ExecStart=VIRTUAL_ENV_DAPHNE -p 9001 MY_PROJECT.asgi:application
#edit myproject
#If your daphne is not installed globally and is inside your #environment, enter the complete path to daphne
#ExecStart=/home/ubuntu/myproject/env/bin/daphne -p 9001
# myproject.asgi:application
[Install]
WantedBy=multi-user.target
有什么问题吗?
请随时询问其他可能负责的设置。