0

I have to set up the Django server with WebSockets and HTTP requests. It works fine in my local system. and also on the server without SSL, but when we use the SSL on the server the HTTP request works fine on the https but WebSocket does not work with wss

I am using Nginx for the webserver and daphne.

this is my Nginx conf file

server {
    listen 80;
    server_name domain.name;

    error_log  /var/log/nginx/error.log;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        autoindex on;
        alias /root/myprojectdir/staff_hiring/static;
    }

        location /media/ {
        autoindex on;
        alias /root/myprojectdir/staff_hiring/media;
    }

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

and this is my asgi file

import os

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import staff_admin.routing
from channels.security.websocket import AllowedHostsOriginValidator
import django

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

django.setup()
application = ProtocolTypeRouter({
    "https": get_asgi_application(),
    'websocket':AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
          staff_admin.routing.websocket_urlpatterns
    ) 
        )
    )
})

and routing file

websocket_urlpatterns = [
  re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer.as_asgi()), # Using asgi
  # url(r'/ws/chat/', consumers.ChatConsumer.as_asgi())

]
swapnil gautam
  • 319
  • 2
  • 9
  • this answer might be helpful: https://stackoverflow.com/questions/12102110/nginx-to-reverse-proxy-websockets-and-enable-ssl-wss –  May 11 '22 at 06:19

0 Answers0