Utkichaps
Utkichaps

Reputation: 129

Not able to connect to websocket using Nginx and Uvicorn

I built a docker container with Django, Uvicorn, Nginx and Redis, and am using django-channels but when I run this it says it cannot connect to the websocket and this is seen in the browser console:

WebSocket connection to 'ws://127.0.0.1:8080/ws/notifications/' failed

It is working fine when I use Django's runserver command for development but when I include Nginx and Uvicorn it breaks.

Entrypoint.sh:

gunicorn roomway.asgi:application --forwarded-allow-ips='*' --bind 0.0.0.0:8000 -k uvicorn.workers.UvicornWorker

Nginx config:

upstream django {
    server app:8000;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 8080;

    location /static {
        alias /vol/static;
    }

    location /ws/ {
        proxy_pass http://0.0.0.0:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

    location / {
        proxy_pass http://django;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_redirect off;
        proxy_buffering off;
    }
}

settings.py:

CHANNEL_LAYERS={
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            'hosts': [('redis', 6379)],  #Redis port
        }
    }
}

The JS file which handles the socket:

var wsStart = "ws://"    
var webSocketEndpoint =  wsStart + window.location.host + '/ws/notifications/'
const notificationSocket = new WebSocket(webSocketEndpoint)

asgi.py:

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": AuthMiddlewareStack(
        URLRouter([
            url(r'^ws/notifications/', NotificationConsumer.as_asgi()),
            path("ws/<str:room_name>/", ChatConsumer.as_asgi())            
        ])
    )
})

Nginx throws this error with the above code:

[error] 23#23: *4 connect() failed (111: Connection refused) while connecting to upstream, server: , request: "GET /ws/notifications/ HTTP/1.1", upstream: "http://0.0.0.0:8000/ws/notifications/", host: "127.0.0.1:8080"

When I change the proxy_pass to http://django instead of 0.0.0.0, Nginx does not throw that error anymore but I get the same error on the console. Also this time Django throws these warnings:

[WARNING] Unsupported upgrade request.       
[WARNING] No supported WebSocket library detected. Please use 'pip install uvicorn[standard]', or install 'websockets' or 'wsproto' manually.

Upvotes: 10

Views: 14210

Answers (3)

GeM
GeM

Reputation: 97

If you use "uvicorn[standart]" maybe cannot see websocket
if occures error like that: install websocket pip install websocket

Upvotes: 0

Dur-e-Maknoon Nisar
Dur-e-Maknoon Nisar

Reputation: 31

pip uninstall uvicorn
pip install "uvicorn[standard]"

Use Double Quotation

Upvotes: 3

Neuron
Neuron

Reputation: 5813

As noted in a comment by Iain Shelvington, it seems like websockets are not included in the base install of uvicorn

pip uninstall uvicorn
pip install 'uvicorn[standard]'

Upvotes: 20

Related Questions