【问题标题】:Django channels and nginx - error during handshakeDjango 频道和 nginx - 握手期间出错
【发布时间】:2020-07-29 11:24:29
【问题描述】:

我正在尝试将 Django/Django Channels 应用程序部署到 VPS。项目的 django 部分有效,我可以访问任何 url 并且正在加载模板,但它的 Django Channels 部分不起作用。每当我尝试访问 websocket 时,我都会得到 connection refusedWebSocket connection to 'ws://54.39.20.155/receiver' failed: Error during WebSocket handshake: Unexpected response code: 404

谁能帮我找出我做错了什么并告诉我我需要做什么才能运行 Django Channels?

这是我的设置:

环境:

virtualenv
django
django-channels
gunicorn
nginx
systemd

/etc/nginx/sites-available/myproject

server {
        listen 80;
        server_name 54.39.20.155;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /WaitingRoomVenv/WaitingRoom/WaitingRoom/static;
        }

        location / {
            include proxy_params;
            proxy_pass http://unix:/WaitingRoomVenv/WaitingRoomEnv.sock;
        }
    }

/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/WaitingRoomVenv/WaitingRoom
ExecStart=/WaitingRoomVenv/WaitingRoomEnv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/WaitingRoomVenv/WaitingRoomEnv.sock WR.wsgi:application

[Install]
WantedBy=multi-user.target

启动 gunicorn:sudo systemctl start gunicorn 启动nginx:sudo systemctl restart nginx

【问题讨论】:

  • 您的频道运营情况如何?你的 WR.wsgi:application 是正常的 django 视图
  • 是的,确实我必须添加另一个 systemd 服务来运行 Daphne,它现在应该可以工作了

标签: django nginx django-channels


【解决方案1】:

添加到你的 nginx.conf

location /receiver {
    proxy_pass http://unix:/WaitingRoomVenv/WaitingRoomEnv.sock;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2019-07-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多