【问题标题】:Unable to implement websocket with Nginix and Daphne无法使用 Nginx 和 Daphne 实现 websocket
【发布时间】:2021-09-25 08:30:16
【问题描述】:

我正在尝试使用 Daphne 和 Ngnix 在我的 django 应用程序上设置 websocket。在我的本地设置中,一切都按预期工作,但是当我上传到服务器时,websockets 没有响应。这是 Nginx.conf 文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##
 access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

        client_max_body_size 10M;
}

这是由 Nginx 访问的我的站点可用文件:

server {
    server_name 139.59.9.118 newadmin.aysle.tech;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/django/AysleServer/src;
    }

     location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    location /wss/ {
        proxy_pass http://0.0.0.1:8001;
        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;
    }




    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/newadmin.aysle.tech/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/newadmin.aysle.tech/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

}
server {
    if ($host = newadmin.aysle.tech) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name 139.59.9.118 newadmin.aysle.tech;
    listen 80;
    return 404; # managed by Certbot


}

这是我的 daphne.service 文件:

  GNU nano 4.8                                                                 daphne.service
[Unit]
Description=WebSocket Daphne Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/home/django/AysleServer/src
#ExecStart=/home/django/AysleServer/MyEnv/bin/python /home/django/AysleServer/MyEnv/bin/daphne -b 0.0.0.0 -p 8001 adminPanel.asgi:application
ExecStart=/home/django/AysleServer/MyEnv/bin/python /home/django/AysleServer/MyEnv/bin/daphne -e ssl:8001:privateKey=/etc/letsencrypt/live/newadmin.aysle.tech/privkey.>
Restart=on-failure

[Install]
WantedBy=multi-user.target

我尝试发送这样的 websocket 请求:

ws://newadmin.aysle.tech/ws/test/
ws://newadmin.aysle.tech:8001/ws/test/

但我没有得到任何回复。我尝试检查日志文件是否有错误,但没有错误。我的猜测是 Nginx 没有将请求转发给 Daphne。应该是配置问题。但我不知道要改变什么。请帮我解决一下这个。提前感谢您的时间。请注意,我也在使用 Gunicorn 来处理 HTTP 请求,它们按预期工作。

【问题讨论】:

    标签: django nginx websocket daphne


    【解决方案1】:

    由于您在 nginx 配置中使用 ssl,因此您还必须使用 wss 而不是 ws 作为方案。

    你的位置也是 /wss/ 所以你的 uri 也应该使用这个位置。

    对来自客户端的请求试试这个:

    wss://newadmin.aysle.tech/wss/test/

    如果这不起作用,您还可以检查您的主机是否允许 WebSockets,或者您是否必须激活它。例如,我使用了 Djangoeurope 服务器,并且必须为 uri 激活 WebSockets。

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2020-06-19
      • 2021-12-23
      • 2021-10-19
      • 2021-03-07
      • 2020-06-09
      • 2018-11-20
      • 2021-08-06
      • 1970-01-01
      相关资源
      最近更新 更多