title: Not able to connect to websocket using Nginx and Uvicorn
body: <p>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:</p>
  <blockquote>
  <p><code>WebSocket connection to 'ws://127.0.0.1:8080/ws/notifications/' failed</code></p>
  </blockquote>
  <p>It is working fine when I use Django's runserver command for development but when I include Nginx and Uvicorn it breaks.</p>
  <p>Entrypoint.sh:</p>
  <pre><code>gunicorn roomway.asgi:application --forwarded-allow-ips='*' --bind 0.0.0.0:8000 -k uvicorn.workers.UvicornWorker
  </code></pre>
  <p>Nginx config:</p>
  <pre><code>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;
      }
  }
  </code></pre>
  <p>settings.py:</p>
  <pre><code>CHANNEL_LAYERS={
      'default': {
          'BACKEND': 'channels_redis.core.RedisChannelLayer',
          'CONFIG': {
              'hosts': [('redis', 6379)],  #Redis port
          }
      }
  }
  </code></pre>
  <p>The JS file which handles the socket:</p>
  <pre><code>var wsStart = &quot;ws://&quot;    
  var webSocketEndpoint =  wsStart + window.location.host + '/ws/notifications/'
  const notificationSocket = new WebSocket(webSocketEndpoint)
  </code></pre>
  <p>asgi.py:</p>
  <pre><code>application = ProtocolTypeRouter({
      &quot;http&quot;: django_asgi_app,
      &quot;websocket&quot;: AuthMiddlewareStack(
          URLRouter([
              url(r'^ws/notifications/', NotificationConsumer.as_asgi()),
              path(&quot;ws/&lt;str:room_name&gt;/&quot;, ChatConsumer.as_asgi())            
          ])
      )
  })
  </code></pre>
  <p>Nginx throws this error with the above code:</p>
  <pre><code>[error] 23#23: *4 connect() failed (111: Connection refused) while connecting to upstream, server: , request: &quot;GET /ws/notifications/ HTTP/1.1&quot;, upstream: &quot;http://0.0.0.0:8000/ws/notifications/&quot;, host: &quot;127.0.0.1:8080&quot;
  </code></pre>
  <p>When I change the <code>proxy_pass</code> to <code>http://django</code> instead of <code>0.0.0.0</code>, Nginx does not throw that error anymore but I get the same error on the console. Also this time Django throws these warnings:</p>
  <pre><code>[WARNING] Unsupported upgrade request.       
  [WARNING] No supported WebSocket library detected. Please use 'pip install uvicorn[standard]', or install 'websockets' or 'wsproto' manually.
  </code></pre>