在生产中为Websocket配置Django通道

我有一个带有... / chat / url的页面,整​​个过程都在localhost上运行。我正在尝试在ubuntu上进行部署,并且遇到了困难。

我猜想到现在为止就像发布我所拥有的:

/ etc / nginx / sites-enabled / mysite:

limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;

upstream channels-backend {
    server localhost:8001;
}

server {
    listen 80;
    server_name foo.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/mysite/mysite/;
    }

    location / {
        include proxy_params;
        limit_req zone=mylimit;
        proxy_pass http://unix:/run/gunicorn.sock;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

我尝试将服务器块更改为包含以下内容:

...
    location / {
        try_files $uri @proxy_to_app;
        include proxy_params;
        limit_req zone=mylimit;
        proxy_pass http://unix:/run/gunicorn.sock;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location @proxy_to_app {
        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;
    }
...

但是这促使我的网页转到503。

我的python和js可以在localhost上正常运行,并且运行daphne -b 0.0.0.0 -p 8001 myproject.asgi:application会生成一条Listening...消息,因此看起来不错。

我的/etc/supervisor/conf.d/mysite.conf是:

[program:mysite_asgi]
directory=/home/ubuntu/mysite/mysite
command=/home/ubuntu/mysite/mysite/venv/bin/daphne -b 0.0.0.0 -p 8001 mysite.asgi:application
autostart=true
autorestart=true
stopasgroup=true
user=ubuntu
stdout_logfile=/home/ubuntu/mysite/daphnelog/asgi.log
redirect_stderr=true

浏览器控制台显示WebSocket connection to 'ws://foo.com/chat/' failed: Error during WebSocket handshake: Unexpected response code: 503,我不确定是否已完整张贴了需要说明的内容以帮助您帮助我-请告知我是否还有其他信息那末。谢谢!

zl19890416 回答:在生产中为Websocket配置Django通道

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2580962.html

大家都在问