Getting trouble while deploying Django ASGI application on ubuntu server. NGINX has been used as webserver

729 Views Asked by At

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.

0

There are 0 best solutions below