WebSocket wss://在SSL上使用Nginx Gunicorn Daphne Channels Redis

pdsfdshx  于 2023-11-17  发布在  Nginx
关注(0)|答案(1)|浏览(177)

我试图让我的Django项目运行WebSockets;在浏览器控制台中,我得到错误
第一个月
settings.py:

CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels_redis.core.RedisChannelLayer",
            "CONFIG": {
                "hosts": [("config('REDIS_SERVER_NAME')", 6379)],
            },
            "ROUTING": "myproject.routing.channel_routing",
        },
    }

字符串
asgi.py:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
application = get_default_application()


nginx配置:

server{
        server_name <IP-adress> <xxx.xxx>;

        location = /favicon.ico { access_log off; log_not_found off; }
        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
        }
        location /ws/ {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;
                proxy_pass http://127.0.0.1:8001/;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/xxx.xxx/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/xxx.xxx/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


daphne.service:

Unit]
Description=WebSocket Daphne Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/home/ubuntu/myproject
ExecStart=daphne -e ssl:8001:privateKey=/etc/letsencrypt/live/xxx.xxx/privkey.pem:certKey=/etc/letsencrypt/live/xxx.xxx/fullchain.pem myproject.asgi:application
Restart=on-failure

[Install]
WantedBy=multi-user.target


该网站工作正常,ws://工作时,我测试它没有SSL证书从certbot的域.任何帮助是赞赏.

41zrol4v

41zrol4v1#

我想明白了!我把我的文件改了如下。
nginx配置:

server{
        server_name mydomain.com www.mydomain.com
        location = /favicon.ico { access_log off; log_not_found off; }
        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
        }

        location /ws/ {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;
                proxy_pass http://unix:/tmp/daphne.sock;
        }
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

字符串
daphne.service:

[Unit]
Description=WebSocket Daphne Service
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/ubuntu/myproject
ExecStart=daphne -u /tmp/daphne.sock myproject.asgi:application
Restart=on-failure

[Install]
WantedBy=multi-user.target

相关问题