1

I have an Django(3.0) app using Daphne as an app container and Nginx as proxy server.

Daphne -u /home/ubuntu/virtualenvs/src/app/app.sock app.asgi:application

My problem is that the websocket connection failed.

(index):16 WebSocket connection to 'ws://example.com/ws/chat/1/' failed: Error during WebSocket handshake: Unexpected response code: 404

I'm pretty sure that my app setting and route is just fine.
Because if I stop Nginx, bind Daphne to 0.0.0.0:8000 and use real IP("ws://xx.xx.xx.xx:8000/ws/chat/1") as URL, the websocket connection established and very stable.
How should I modify my Nginx to make websocket work?

#my nginx setting

upstream websocket {
    server unix:/home/ubuntu/virtualenvs/src/app/app.sock;
}
#websocket settings
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
listen 80;
listen [::]:80 default_server;
listen 443 SSL default_server;
listen [::]:443 SSL default_server;
server_name example.com;
return 301 https://example.com$request_uri;
ssl on;
# certificate settings
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_session_timeout 10m;
#ssl_session cache shared:SSL:1m;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
#ssl_prefer_server_ciphers on;
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!$

location / {
    root /home/ubuntu/virtualenvs/
    include proxy_params;
    proxy_pass https://websocket;

    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-Proto $scheme;
    proxy_set_header X-Frame-Options SAMEORIGIN;
    proxy_pass_request_headers on;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    }

location ~* \.(js|css)$ {
    expires -1;
  }
}
user2869934
  • 1,419
  • 4
  • 13
  • 18
  • You can bind Daphne to port (:8000) and in nginx proxy to that localhost port, either with http or websockets. (http://127.0.0.1:8000 / ws://127.0.0.1:8000). – Oleg Russkin Jan 13 '20 at 10:41
  • Is socket file app.sock being created on daphne start? Mb it is permission issue. – Oleg Russkin Jan 13 '20 at 10:41
  • @OlegRusskin The app.sock was created. http requests are fine but websocket request returned 404 (Not Found: /ws/chat/1/) – user2869934 Jan 14 '20 at 02:22
  • @OlegRusskin I wonder if it's possible that I didn't handle the route right. But it work just fine while URL with real IP is the host name. Can't figure it out... – user2869934 Jan 14 '20 at 02:24
  • @OlegRusskin Thanks for the information. Tried the combination "proxy_pass http://localhost:8000" in nginx and "daphne -b localhost app.asgi:application". I get error 502 Bad gateway. – user2869934 Jan 14 '20 at 02:27
  • Why proxy_pass is to `https`? Use `http://websocket`. You can also add `proxy_redirect off;` option. – Oleg Russkin Jan 14 '20 at 05:53
  • Thanks a lot. But I finally found what the problem is... I was editing /etc/nginx/site-available/app rather than /etc/nginx/site-enabled/default. That's why my modifications did not work at all. Can't believe what a fool I am. Trying fixing this problem for a month... Thanks for your help. – user2869934 Jan 14 '20 at 06:30

0 Answers0