Score:1

Have NGINX automatically upgrade websocket connections in reverse proxy

in flag

I have several services I'm placing behind an NGINX reverse proxy, which was simple enough to setup, but I've run into a problem with websockets. A single endpoint is simple enough to specify with a location that includes

   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";

The problem is that there are many endpoints with websockets. And we keep finding new things that don't work because there's yet another websocket endpoint we didn't know about.

Is there a way to only set the Upgrade and Connection header if the client passes it? If I include those two lines for the whole service it attempts to upgrade every connection, not just the websockets.

Score:1
in flag

Just had the same problem, and was battling if statements in nginx configuration and realized these directives:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Don't do anything unless the client set an Upgrade header.

So instead of adding specific Location directive for each websocket endpoint, you can just make a global one:

server {
  listen          443 ssl;
  server_name     my-server.example
  location / {
    proxy_pass http://local-service.example/;
    proxy_http_version 1.1;
    proxy_read_timeout 86400s;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

  }
}

This proxy's any HTTP request and also makes websockets work for the entire namespace. The reason it works is because $http_upgrade is empty for non-websocket requests.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.