WebSocket Connection Issue with Django Channels in Gunicorn

I tried to implement live chat using Django Channels and Daphne. It works fine on my local server, but now I want to implement it in production using Gunicorn, nginx. However, when I reload and restart the and run, I'm getting an error (as shown in the image below) it in service. I tried modifying my services, but it still doesn't work. I can't figure out the cause since there is no specific error. I read some documentation should I necessary to makemigrations? Any ideas would be much appreciated.

enter image description here

this is what I tried

Service Gunicorn

[Unit]
Description=Tris application daemon services
Requires=tev.socket
After=network.target

[Service]
User=dswdcaraga
Group=www-data
WorkingDirectory=/opt/apps/tev
Environment=DJANGO_SETTINGS_MODULE=tev.settings
ExecStart=/opt/apps/env/bin/daphne -u /run/tev/tev.sock tev.asgi:application --bind 0.0.0.0:8000

[Install]
WantedBy=multi-user.target

Nginx

server {
server_name caraga-tris-staging.dswd.gov.ph;
listen 80;
return 301 https://caraga-tris-staging.dswd.gov.ph$request_uri;
}
server {
    server_name caraga-tris-staging.dswd.gov.ph;
    listen 443 ssl;

    #ssl_certificate /etc/ssl/certs/caraga/server-cer.pem;
    #ssl_certificate_key /etc/ssl/certs/caraga/server-key.key;

    ssl_certificate /etc/ssl/certs/caraga/ssl_nginx/server-cer.pem;
    ssl_certificate_key /etc/ssl/certs/caraga/ssl_nginx/server-key.key;


    location / {
        include proxy_params;
        proxy_pass http://unix:/run/tev/tev.sock;

        #WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        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_headers_hash_max_size 1024;
    proxy_headers_hash_bucket_size 128;

asgi.py configuration

import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
#from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack
from main import routing
import main.routing

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tev.settings')

django.setup()

django_asgi_app = get_asgi_application()

#application = get_asgi_application()
application = ProtocolTypeRouter({
    #"http": get_asgi_application(),
    "http": django_asgi_app,
    "websocket": URLRouter(
        main.routing.websocket_urlpatterns
    )
})

You just need to move these lines on the top of your asgi.py file.

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")

import django

django.setup()

and don't forget to route your sockets in your nginx service to the /ws url:

location /ws/ {
        proxy_pass http://unix:/run/daphne/daphne.sock;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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;
    }
Back to Top