1

I have been on this for a month now without a working solution. Everything works fine in production but I have been trying to deploy my django-channels application using nginx as reverse proxy, supervisor to keep servers running, gunicorn to serve http requests and I am stuck at the weboscket request part using daphne to process http requests.

I am bindig with unix sockets: gunicorn.sock and daphne.sock The Console returns:

WebSocket connection to 'ws://theminglemarket.com/ws/chat/undefined/' failed: 
Error during WebSocket handshake: Unexpected response code: 500

My supervisor config:

directory=/home/path/to/src
command=/home/path/to/venv/bin/gunicorn_start
user=root
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/path/to/log/gunicorn/gunicorn-error.log

[program:serverinterface]
directory=/home/path/to/src
command=/home/path/to/venv/bin/daphne -u /var/run/daphne.sock chat.asgi:application
autostart=true
autorestart=true
stopasgroup=true
user=root
stdout_logfile = /path/to/log/gunicorn/daphne-error.log

Redis server is up and Running, Sure of that, using redis-server my nginx configurations:

upstream channels-backend {
#   server 0.0.0.0:8001;
    server unix:/var/run/daphne.sock fail_timeout=0;
}
upstream app_server {
    server unix:/var/run/gunicorn.sock fail_timeout=0;
}
server {
    listen 80;
    listen [::]:80;
    
    server_name theminglemarket.com www.theminglemarket.com;
    keepalive_timeout 5;
    client_max_body_size 4G;
    access_log /home/path/to/logs/nginx-access.log;
    error_log /home/path/to/logs/nginx-error.log;


    location /static/ {
        alias /home/path/to/src/static/;
        # try_files $uri $uri/ =404;
    }
    location / {
        try_files $uri @proxy_to_app;
    }
    location /ws/ {
        try_files $uri @proxy_to_ws;
    }
    location @proxy_to_ws {
        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;
    }
    location @proxy_to_app {
        proxy_pass http://app_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        # we don't want nginx trying to do something clever with
        # redirects, we set the Host: header above already.
        proxy_redirect off;
    }
}

Please ask for any other thing needed, I'll update as quickly as I can. Thank You. It's a chatting application, do you think I should use only Daphne, I'm considering the scalability, and that's why I used gunicorn to serve http requests. Hosting on Ubuntu Server

1 Answers1

0

Try putting socket=tcp://0.0.0.0:8001 or socket=tcp://localhost:8001 in your [program:serverinterface] part of supervisord.conf. After that read your supervisor_log.log file to find out how it behaves. I had similar problems with it too. I hope that this helps. Use socket=tcp://localhost:8001 if it's inside of docker container. And make sure that nginx container is on the same docker network as that container.