0

I am facing a lot of trouble while deploying my Django ASGI application. I have tried all the solutions out there.

In my ngix/error.log file, the error is:

2020/11/05 21:37:48 [error] 6716#6716: *31 connect() failed (111: Connection refused) while connecting to upstream, client: 103.144.89.98, server: 156.67.219.10, request: "GET /ws/chat/bbd7182cd0ee95488f1a1e6f3fe0d8f94ed0d14e4db1dce713fe82a3231c523d/ HTTP/1.1", upstream: "http://[::1]:9001/ws/chat/bbd7182cd0ee95488f1a1e6f3fe0d8f94ed0d14e4db1dce713fe82a3231c523d/", host: "156.67.219.10"

In web-browser console, I am getting the following error:

WebSocket connection to 'ws://156.67.219.10/ws/chat/bbd7182cd0ee95488f1a1e6f3fe0d8f94ed0d14e4db1dce713fe82a3231c523d/' failed: Error during WebSocket handshake: Unexpected response code: 400

Here is my settings.py file:

WSGI_APPLICATION = 'thinkgroupy.wsgi.application'
ASGI_APPLICATION = "thinkgroupy.routing.application"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("0.0.0.0", 6379)],
        },
    },
}

Here is the file of nginx configuration: /etc/nginx/sites-available/thinkgroupy:

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

server {
    listen 80;
    server_name 156.67.219.10;
    client_max_body_size 4G;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        alias /home/admin/thinkgroupy/staticfiles/;
    }
    location /media/ {
        root /home/admin/thinkgoupy;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
    #path to proxy my WebSocket requests
    location /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;
    }
}

Here is the supervisor file: /etc/supervisor/conf.d/thinkgroupy.conf:

[program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:9001

# Directory where your site's project files are located
directory=/home/admin/thinkgroupy

# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=/home/admin/thinkgroupy/venv/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers thinkgropy.asgi:application>

# Number of processes to startup, roughly the number of CPUs you have
numprocs=4

# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d

# Automatically start and recover processes
autostart=true
autorestart=true

# Choose where you want your log to go
stdout_logfile=/var/log/asgi.log
redirect_stderr=true

I have followed the django-channels official documentation of deploying ASGI application. Still I am getting the same issues.

Hope any expert will help me to find out the solution.

Farid Chowdhury
  • 2,766
  • 1
  • 26
  • 21
  • Thanks for your reply. here is the official documentation that I followed: https://channels.readthedocs.io/en/stable/deploying.html#nginx-supervisor-ubuntu It uses `tcp://` there – Farid Chowdhury Nov 05 '20 at 23:48
  • Ugh, my bad. I read too fast and didn't see that supervisor creates the socket. Can you pin them both on 127.0.0.1 instead of localhost, to rule out one is using ipv6 and the other is not? –  Nov 05 '20 at 23:57
  • I have changed the `localhost` to `127.0.0.1` in supervisor file and nginx configuration file. Still getting the same error. – Farid Chowdhury Nov 06 '20 at 00:13
  • Then daphne must not be starting. Anything in asgi.log? Probably better to set autorestart to false, till you figure out where the problem is. That `>` at the end is a typo here, not in your config, I assume. The nginx/supervisor config looks ok, so must be something in the django layer. –  Nov 06 '20 at 00:31
  • Yeah, there was a typo, I didn't notice that earlier. Thanks for that. But still I am facing the issue. Right now I am getting the following error in browser console is: `WebSocket opening handshake timed out` – Farid Chowdhury Nov 06 '20 at 13:28

0 Answers0