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

时间:2019-01-07 16:37:05

标签: python django ubuntu nginx django-channels

我正在使用Ubuntu 18.04 LTS,Django 2.1,Python 3.6,Nginx,Gunicorn,我通过以下链接复制了测试项目:https://channels.readthedocs.io/en/latest/tutorial/part_1.html

我根据以下说明部署了ubuntu服务器:https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#troubleshooting-nginx-and-gunicorn

但是我收到了404错误。     WebSocket与“ ws://54.184.201.27/ws/chat/lobby/”的连接失败:WebSocket握手时出错:意外的响应代码:404聊天套接字意外关闭

我从这里发布的github问题中得到了一些想法:https://github.com/socketio/socket.io/issues/1942,答案是我应该在conf文件中添加一些代码。但是我仍然遇到错误。

/etc/nginx/sites-available/project.conf

server {
    listen 80;
    server_name 54.184.201.27;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/django-channels/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
        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;


    }

}

添加proxy_set_header主机$ host后,出现错误:

Invalid HTTP_HOST header: '54.184.201.27,54.184.201.27'. The domain name provided is not valid according to RFC 1034/1035.

我没有将proxy_set_header主机$ host放在这里,因为它会给出错误的IP地址错误。任何解决问题的想法吗?

似乎因为我使用了gunicorn,所以我不应该放置可能重复的代理设置。

0 个答案:

没有答案