nginx not upgraded to websocket

38 views Asked by At

i have a Kubernetes cluster in which nginx:1.21.6 is running as a proxy.

Nginx should upgrade the connection to websocket at path /ws/ and forward it to the service http://dev-websocket:8000.

Since some changes, however, the dev-websocket service seems to receive an HTTP connection. But I can't find the problem.

django channels runs on dev-websocket behind daphne. The logs in the dev-websocket service should actually look like this:

10.2.0.22:12345 - - [09/Feb/2024:11:00:00] "WSCONNECTING /ws/notifications/" - -
10.2.0.22:12345 - - [09/Feb/2024:11:00:00] "WSCONNECT /ws/notifications/" - -

However, the output is currently:

10.2.0.22:12345 - - [09/Feb/2024:11:00:00] "GET /ws/notifications/" 302 -

My nginx.conf currently looks like this:

pid /tmp/nginx.pid;
worker_processes  1;
events {
  worker_connections  10240;
}
http {
  include /etc/nginx/mime.types;
  client_max_body_size 20m;
  
  gzip on; 
  gzip_vary on; 
  gzip_min_length 1024; 
  gzip_proxied expired no-cache no-store private auth; 
  gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; 
  gzip_disable "MSIE [1-6]\.";

  server {
      listen 8080 default_server;
      server_name _;

      location / {
        proxy_pass http://dev-normalservice:8000/;
      }
      
      location /ws/ {
        proxy_pass http://dev-websocket:8000;
      
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
      }

      location /files/static/ {
        alias /files/static/;
        try_files $uri =404;
      }
      
      location /internal/ {
        internal;
        alias /files/private/;
        try_files $uri =404;
      }
      
      location /healthcheck/ {
        access_log off;
        add_header 'Content-Type' 'text/plain';
        return 200;
      }
  }
}

Why does nginx not seem to switch the connection to websocket and forwards the request as http to the dev-websocket service?

0

There are 0 answers