CodexBloom - Programming Q&A Platform

Django Channels not handling WebSocket connections properly in production

👀 Views: 1 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-09
django channels websockets nginx redis Python

I've looked through the documentation and I'm still confused about I'm stuck on something that should probably be simple. I'm using Django Channels 3.1 with Redis as the channel layer to manage WebSocket connections in my application. Everything works perfectly in my local development environment, but when I deploy to production, I'm working with an scenario where WebSocket connections intermittently unexpected result with a `ConnectionClosed` behavior. I can see in my logs that the server is throwing this behavior: ```plaintext WebSocket connection closed: 1006 ``` I've verified that my Redis server is configured correctly and is accessible from my production app. In my `settings.py`, I have the following configurations: ```python CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels_redis.core.RedisChannelLayer', 'CONFIG': { 'hosts': [('127.0.0.1', 6379)], }, } } ``` I also use Daphne as my ASGI server. My `Procfile` is set up like this: ```plaintext web: daphne -u /tmp/daphne.sock myproject.asgi:application ``` In production, I'm using Nginx as a reverse proxy and I've set it up to handle WebSocket connections: ```nginx location /ws/ { proxy_pass http://unix:/tmp/daphne.sock; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $host; } ``` Despite all these configurations, users are still experiencing dropped connections. I've tried increasing the timeout settings in both Nginx and Daphne, but the question continues. Could this be related to network issues or something specific to the production setup? Any advice on how to debug this or potential solutions would be greatly appreciated. I'm using Python stable in this project. I appreciate any insights! The project is a web app built with Python.