JIYIK CN >

Current Location:Home > Learning > NETWORK >

How to fix the Unknown "connection_upgrade" Variable error in Nginx

Author:JIYIK Last Updated:2025/03/17 Views:

When working with Websockets or configuring a server with nginx, we may come across $connection_upgradevariables in the nginx configuration.

The $connection_upgrade variable is not available by default. However, it is recommended to define and use it in a reverse proxy setup.

This article will show you how to fix the nginx unknown variable error related to connection upgrades!

Unknown "connection_upgrade" Variable error

We may nginx -tencounter this problem when checking our nginx configuration (after an update) using:

$ sudo nginx -t

nginx: [emerg] unknown "connection_upgrade" variable  
nginx: configuration file /etc/nginx/nginx.conf test failed  

connection_upgradeThe variable is not a global nginx setting. However, we'll see it in tutorials and code snippets all over the Internet. Even nginx Inc. recommends defining and using connection_upgrade. Let's start fixing it!


Configure the “$connection_upgrade” variable

Connection upgrades are often used in conjunction with WebSockets. In nginx, we can $http_upgradeupgrade HTTP connections to WebSocket connections based on the variables.

We can define the dependency between connect and http upgrade in nginx using the map block:

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

If the Upgrade header is set to '', this map block tells nginx to properly set the relevant Connection headers to close the connection.

Place the map block into the http block of the nginx configuration. The default file path for the nginx configuration is /etc/nginx/nginx.conf .

This is an example nginx configuration using a map block that defines the $connection_upgrade variable.

/etc/nginx/nginx.conf

user www-data;  
worker_processes auto;  
pid /run/nginx.pid;

events {  
        multi_accept       on;
        worker_connections 65535;
}

http {  
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        …

        ##
        # Connection header for WebSocket reverse proxy
        ##
+       map $http_upgrade $connection_upgrade {
+           default upgrade;
+           ''      close;
+       }

        # further configurations …
}

Save the updated nginx configuration file. Then, check the configuration file again using nginx -t:

$ sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok  
nginx: configuration file /etc/nginx/nginx.conf test is successful  

We can see that the configuration has taken effect. ✌️


Using the “$connection_upgrade” variable

The following example shows how to use the newly defined $connection_upgradevariable. It is used in conjunction with the proxy header. Setting the proxy header in our location block looks like this:

server {  
    listen 80;
    listen 443 ssl http2;

    location / {
        proxy_http_version             1.1;
        proxy_set_header Upgrade       $http_upgrade;
        proxy_set_header Connection    $connection_upgrade;

        proxy_pass http://127.0.0.1:1234;
    }
}

That’s it, you have nginx configured to support WebSocket connections!

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Nginx load balancing settings

Publish Date:2025/03/18 Views:198 Category:NETWORK

At this stage, load balancing is a widely used technology. Nginx, as a load balancing server for http, is being used more and more widely. There are three ways to set up Nginx load balancing: Round-robin - This method distributes access req

Nginx load balancing health_check analysis

Publish Date:2025/03/18 Views:54 Category:NETWORK

In Nginx load balancing, it is difficult to guarantee that every application server can run normally all the time. However, we can set Nginx to detect these application servers and detect which of them are inaccessible. There are two ways t

HTTP2 Tutorial - How to Configure HTTP2 with Nginx

Publish Date:2025/03/17 Views:195 Category:NETWORK

HTTP2 was officially released in 2015. If your website is still using HTTP/1.1, you may be out of date. Don't worry, here we will see how to use Nginx to upgrade your website to HTTP2. Install Nginx I feel that this column is redundant. Sin

Deep understanding of Nginx's server block selection algorithm

Publish Date:2025/03/17 Views:95 Category:NETWORK

Nginx is one of the most popular web servers in the world. It can successfully handle high loads with many concurrent client connections and can be used as a web server, mail server, or reverse proxy server. In this article, we will discuss

In-depth understanding of Nginx Location block matching algorithm

Publish Date:2025/03/17 Views:61 Category:NETWORK

Similar to the process that Nginx uses to select the Server block that will handle a request  , Nginx also has an established algorithm to decide which Location block within a Server block to use to handle a request. location block syntax

Nginx is running but not serving sites

Publish Date:2025/03/17 Views:86 Category:NETWORK

We recently installed nginx version 1.17 on a new machine. The configuration created in sites-available` was symlinked to `sites-enabled`, but nginx is not serving any domains.

How Nginx and uWISG servers work together

Publish Date:2025/03/17 Views:70 Category:NETWORK

Nginx and uWISG are two commonly used server software that can work together to provide more stable and efficient network services. This article will introduce in detail the working principle of Nginx and uWISG, and how to configure them to achieve op

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial