Nginx / gunicorn WebSocket连接失败:WebSocket握手时出错:意外的响应代码:404/400

时间:2019-09-18 10:55:19

标签: nginx websocket gunicorn uvicorn

所以我将 Centos 7,Django 2.1.3,Python 3.6,Nginx,Gunicorn,uvicorn,Channels 用于聊天项目。
我在Centos服务器上部署了该项目:我用gunicorn服务运行该项目。
现在,当我像这样ws://xx.xxx.x.xx:82/ws/chat/使用server_ip测试WebSocket连接时 :效果很好,但是当我像wss://myDomaineName/ws/chat/这样使用该IP的域名时:我收到此错误(failed: Error during WebSocket handshake: Unexpected response code: 404
为什么在使用myDomaineName时放置 wss 而不是 ws 的原因,因为我有一个带SSL的WAF,可用于重定向到项目服务器。
这是我的 /etc/nginx/conf.d 中的nginx conf文件:

map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
}

upstream uvicorn {
   server unix:/srv/www/myProject/run/gunicorn.sock fail_timeout=10s;  
}

server {
    listen   82;
    access_log /srv/www/myProject/logs/nginx-access.log;
    error_log /srv/www/myProject/logs/nginx-error.log warn;
    root   /srv/www/myProject/back-end;

    location /ws/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://uvicorn;   
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_buffering off;
    }


}

我非常确定问题出在Nginx conf中,因为当我测试时,我在nginx-access.log中得到了这个:
xx.xxx.x.xx - - [18/Sep/2019:11:37:05 +0100] "GET /ws/chat/ HTTP/1.1" 404 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36"
我在nginx-error.log中什么也没得到。
我已经阅读了许多类似Nginx/Apache2 WebSocket connection to failed: Error during WebSocket handshake: Unexpected response code: 404https://github.com/socketio/socket.io/issues/1942的问题,并且多次更改了配置,但是遇到Unexpected response code: 400时出现相同的错误或相同的错误。对解决问题有任何想法吗?

0 个答案:

没有答案