1

Base on what I could read on nginx site https://www.nginx.com/blog/websocket-nginx/

The exemple they give will close all connections to backend. This isn't really what we want on a proxy setup, forcing to reopen a connection to backend on each new client.

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
 
    upstream websocket {
        server 192.168.100.10:8010;
    }
 
    server {
        listen 8020;
        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
        }
    }
}

But is it required to close the connection after upgrading to a websocket?

can't we change the map so it maintains the 'keepalive' connection to the backend ? (for all non websocket requests)

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

Being the only user on a test environment it didn't seem to create any issues, but will it be the same in production?

Antony Gibbs
  • 425
  • 2
  • 12

1 Answers1

0

This map does not cause websocket connections to close.

What it actually does is check whether the Upgrade: request header contains any value. If it does, then it returns upgrade, which is then passed upstream as the header Connection: upgrade.

It only returns close when the Upgrade: request header is missing. This should not happen in normal operation, but if it does happen, then you can't reliably (or at all) establish a websocket connection to the browser anyway.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • `It only returns close when the Upgrade: request header is missing.` but then that is what happens on any classic connection (that are not websocket). Prior to enabling websocket I had a line `proxy_set_header Connection ''` to not let the client 'close' terminate the keepalive with backend. – Antony Gibbs Jul 21 '20 at 12:39
  • 1
    @AntonyGibbs You aren't going to send normal connections to your websocket! You should only send websocket connections to your websocket URL. Which means your `location` needs to have the correct path. – Michael Hampton Jul 21 '20 at 13:40