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

How to Install Nginx on Ubuntu 20.04?

Publish Date:2025/04/07 Views:157 Category:OPERATING SYSTEM

Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most trafficked sites on the Internet. It is a lightweight application software that can be used as a web server or a reverse proxy.

Install WordPress with Nginx on Ubuntu 18.04

Publish Date:2025/04/07 Views:86 Category:OPERATING SYSTEM

WordPress is one of the most popular open source content management systems (CMS) with a market share of up to 60% compared to other CMS like Drupal or Joomla. WordPress can be used to develop any type of website, be it a blog, a small busi

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

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:56 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:98 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

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial