问题
I am using Ubuntu 18.04 LTS, Django 2.1, Python 3.6, Nginx, Gunicorn and the testing project I copied from is through this link: https://channels.readthedocs.io/en/latest/tutorial/part_1.html
I deployed the ubuntu server based on the instructions here: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#troubleshooting-nginx-and-gunicorn
But i got the 404 error. WebSocket connection to 'ws://54.184.201.27/ws/chat/lobby/' failed: Error during WebSocket handshake: Unexpected response code: 404 Chat socket closed unexpectedly
I got some idea from the github issues posted here: https://github.com/socketio/socket.io/issues/1942 and answers are that I should add some code to my conf file. But I still got the error.
/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;
}
}
After I add the proxy_set_header Host $host, I got the error:
Invalid HTTP_HOST header: '54.184.201.27,54.184.201.27'. The domain name provided is not valid according to RFC 1034/1035.
I didn't put the proxy_set_header Host $host here as it will gave the error of wrong ip address. Any idea of how to solve the problem?
It seems like since i used gunicorn, i should not put those proxy setting which might be duplicated.
来源:https://stackoverflow.com/questions/54078334/nginx-apache2-websocket-connection-to-failed-error-during-websocket-handshake