Django channels web socket not working with nginx and gunicorn in the cloud server

19 views Asked by At

This is my project configuration for Django channels, gunicorn and nginx.

In local machine the django websocket works great. but after deploying to the cloud server it shows this console error:

Mar 27 08:50:03 my-server-ip gunicorn[3426841]:  - "GET /ws/notifications/?token=mytoken-eyJhbGciOi HTTP/1.0" 404

It seems, it cant find the ws/notifications path. my app routings.py:

from django.urls import pathfrom EchoWave.consumers import NotificationSendConsumer
websocket_urlpatterns = [
        path("ws/notifications/", NotificationSendConsumer.as_asgi(), name="notificationconsumer"),
]

is there any issue in the nginx or gunicorn configuration?

My other configurations related to websocket

asgi.py

import os 
import django 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app_core.settings') django.setup()

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from app_core.middlewares.request_logging_middleware import ChannelsLoggerMiddleware
import EchoWave.routing

application = ProtocolTypeRouter(
    {
        "http": ChannelsLoggerMiddleware(get_asgi_application()),
        "websocket": AllowedHostsOriginValidator(
            AuthMiddlewareStack(
                URLRouter(
                    EchoWave.routing.websocket_urlpatterns
                )
            ),
        ),
    }
)

nginx.conf

map $http_origin $allow_origin {
    ~^https?://(localhost:.*|.*\.mydomain\.com) $http_origin;
    default "";
}


server {
    listen 443 ssl;

    ssl_certificate /etc/ssl/mydomain-bundle.crt;
    ssl_certificate_key /etc/ssl/mykey.key;

    server_name admin.mydomain.com www.admin.mydomain.com;


    gzip on;
    gzip_comp_level    9;
    gzip_min_length    256;
    gzip_proxied       any;
    gzip_vary          on;

    root /var/www/html/Backend/dist;
    index index.htm;

    server_name front.mydomain.com www.front.mydomain.com;

    location /static {
        alias /var/www/html/Backend/static;
    }

    location /media {
        add_header 'Access-Control-Allow-Origin' $allow_origin;
        add_header 'Vary' "Origin";
        alias /var/www/html/Backend/media;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
        
        access_log /var/www/html/Backend/logs/dev-nginx-access.log;
        error_log /var/www/html/Backend/logs/dev-nginx-error.log;
    }

    location /ws/ {
        proxy_pass http://unix:/run/gunicorn.sock;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/html/Backend
ExecStart=/var/www/html/Backend/env/bin/gunicorn \
            --access-logfile - \ 
            -k uvicorn.workers.UvicornWorker \ 
            --workers 3 --bind \
            unix:/run/gunicorn.sock app_core.asgi:application 

[Install]
WantedBy=multi-user.target

Why is the websocket url being called as http GET method? Thanks in advance.

0

There are 0 answers