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

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

我们最近在一台新机器上安装了 nginx 版本 1.17。 在 sites-available`中创建的配置被符号链接到 `sites-enabled` ,但 nginx 没有为任何域名提供服务。

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

Nginx 和 uWISG 服务器之间如何配合工作的

Publish Date:2023/03/29 Views:234 Category:网络

Nginx和uWISG是两个常用的服务器软件,它们可以协同工作以提供更加稳定和高效的网络服务。本文将详细介绍Nginx和uWISG之间的配合工作原理,以及如何配置它们以实现最佳性能。 一、

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial