Search code examples
nginxdaphne

Nginx - Daphne deployment issue


I recently added a function utilizing WebSocket with Channels to Django web application and having some trouble. Since the Channels and WebSocket work just fine with the local test server (manage.py runserver), one can tell that the deployment part is responsible.

Here are some setting files and status checks:

nginx_conf.conf

#added this block
upstream channels-backend {
    server localhost:9001;
}

server {
    listen 80;
    server_name MY_URL;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/MY_USER/server/mysite;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/MY_USER/server/mysite/mysite.sock;
    }

    #path to proxy my WebSocket requests
    location /ws/ {

 #       proxy_pass http://unix:/home/MY_USER/server/mysite/mysite_w.sock;
        proxy_pass http://channels-backend;
        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;
    }
}

daphne.service

[Unit]
Description=daphne daemon
After=network.target


[Service]
User=MY_USER
Group=www-data
WorkingDirectory=/home/MY_USER/server/mysite
#ExecStart=/home/MY_USER/server/server/bin/daphne -u /home/MY_USER/server/mysite/mysite_w.sock  mysite.asgi:application -v2
ExecStart=/home/MY_USER/server/server/bin/daphne -p 9001 mysite.asgi:application

[Install]
WantedBy=multi-user.target

As you can see I've tested both port and UNIX socket for binding and both are not successful.

  1. PORT case
    Chrome console message
(index):16 WebSocket connection to 'ws://MY_URL/ws/chat/test/' failed: Error during WebSocket handshake: Unexpected response code: 400
(anonymous) @ (index):16
(index):30 Chat socket closed unexpectedly
chatSocket.onclose @ (index):30
2(index):43 WebSocket is already in CLOSING or CLOSED state.

❯ sudo less /var/log/nginx/access.log

61.82.112.1 - - [12/Aug/2020:06:27:29 +0000] "GET /chat/test/ HTTP/1.1" 200 682 "http://MY_URL/chat/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"
61.82.112.1 - - [12/Aug/2020:06:27:30 +0000] "GET /ws/chat/test/ HTTP/1.1" 400 5 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"

❯ sudo journalctl -u mysite-daphne.service

Aug 10 09:31:32 hermes systemd[1]: mysite-daphne.service: Succeeded.
Aug 10 09:31:32 hermes systemd[1]: Stopped daphne daemon.
Aug 10 09:31:32 hermes systemd[1]: Started daphne daemon.

Which looks like Daphne did not get the message from Nginx.

  1. UNIX socket case
    Chrome console message
(index):16 WebSocket connection to 'ws://MY_URL/ws/chat/test/' failed: Error during WebSocket handshake: Unexpected response code: 400
(anonymous) @ (index):16
(index):30 Chat socket closed unexpectedly
chatSocket.onclose @ (index):30
3(index):43 WebSocket is already in CLOSING or CLOSED state.
document.querySelector.onclick @ (index):43
document.querySelector.onkeyup @ (index):36

❯ sudo less /var/log/nginx/access.log

61.82.112.1 - - [12/Aug/2020:06:42:44 +0000] "GET /ws/chat/test/ HTTP/1.1" 400 5 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Mobile Safari/537.36"

❯ sudo journalctl -u mysite-daphne.service

Aug 10 09:31:32 hermes systemd[1]: Stopped daphne daemon.
Aug 10 09:31:32 hermes systemd[1]: Started daphne daemon.

I need some advice for troubleshooting. Please feel free to ask any extra info which may be helpful.

Thank you in advance.


Solution

  • I had exactly the same problem and was starting to go mad.

    Your nginx config contains the same wrong statement as mine did:

    [...]
    proxy_set_header Connection “upgrade”;
    [...]
    

    The word upgrade is enclosed by the wrong quotes. You need to have standard quotes here (ASCII code 34, Shift-2 on the keyboard), not the "fancy" unicode quotes. Very very hard to find.

    Some websites seem to convert standard quotes to unicode shifted quotes because someone thinks they look better...