Running daphne behind nginx reverse proxy with protocol upgrade always routes to http instead of websocket

Jagadesh Babu T :

I am trying to deploy django channels powered notification application behind Nginx reverse proxy server to only serve the websocket communications, while having Nginx + uWSGI setup to serve the django application.

The application works in my local machine seamlessly when run with either python manage.py runserver --noasgi + daphne -p 8000 myproject.asgi:application or python manage.py runserver with daphne handling all the requests internally.

Problem:

All the websocket requests are routed to http protocol type instead of websocket protocol type and it returns WebSocket connection to 'ws://ip_address/ws/' failed: Error during WebSocket handshake: Unexpected response code: 404

Packages Installed:

Django==2.0.3
channels==2.0.2
channels-redis==2.1.1
daphne==2.1.0
asgiref==2.3.0
Twisted==17.9.0
aioredis==1.0.0
autobahn==18.4.1

Environment:

Ubuntu - 16.04
Nginx - 1.12.0

Nginx Configuration for upgrading the request:

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

# the upstream component nginx needs to connect to websocket requests
upstream websocket {
    server unix:/path/to/my/app/daphne.sock;
}

# configuration of the server
server {

        # the port your site will be served on
        listen      80;

        # the domain name it will serve for
        server_name ip_address;
        charset     utf-8;

       # Sending all non-media requests for websockets to the Daphne server.
        location /ws/ {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }

}

routing.py

from django.conf.urls import url

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from myapp import consumers

application = ProtocolTypeRouter({
    'websocket': AuthMiddlewareStack(
        URLRouter([
            url(r'^ws/$', consumers.MyConsumer),
        ])
    ),
})

Daphne Logs:

None - - [TimeStamp] "GET /ws/" 404 3
None - - [TimeStamp] "GET /ws/" 404 3
None - - [TimeStamp] "GET /ws/" 404 3

Kindly let me know if anything in addition is required to help.

P.S: I have deployed the same application in two servers(both having the same configuration and environment as above) and the result was same.

Jagadesh Babu T :

Finally I found that the culprit was my company's firewall, which was stripping the upgrade headers when application was accessed over http. So after upgrading http to https, it started working as expected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to host websocket behind nginx reverse proxy

Kubernetes Ingress running behind nginx reverse proxy

Nginx Reverse Proxy Websocket Authentication - HTTP 403

Angular App running on nginx and behind an additional nginx reverse proxy

nginx reverse proxy redirects to http instead of https

Laravel routes behind reverse proxy

Running Go server behind Nginx Reverse Proxy with SSL

grafana behind a nginx reverse proxy

Using Daphne behind nginx

NGINX reverse proxy to HTTP

Nginx Reverse Proxy WebSocket Timeout

SvelteKit routes go to redirect loop when put behind NGINX reverse proxy

Running NGINX as a Reverse Proxy on Docker?

Kubectl proxy behind NGINX: invalid upgrade response

Flask - SocketIO hidden behind reverse proxy with Nginx

Minio console not accessible behind nginx reverse proxy

Configuration for Piwik behind nginx reverse proxy with rewrite

Routes from DB for nginx reverse proxy

Urls of a symfony app running behind a reverse proxy

Nginx - Reverse Proxy a WebSocket server with multiple paths

Unable to connect to Websocket Server with Nginx reverse proxy

Using WebSocket Secure with Nginx and Daphne

Nginx reverse proxy instead of directory listing

Running unicorn server as daemon (reverse proxy to nginx)

Running Apache Zeppelin with nginx as reverse proxy

Nginx reverse proxy doesnt not appear to be working when Node http-server running

Docker Flask app behind nginx reverse-proxy sending 404

Wordpress on Docker behind nginx reverse proxy using SSL

Server behind nginx reverse proxy ignores relative path in URL