Nginx 如何修复 Unknown "connection_upgrade" Variable 错误
在使用 Websockets 或使用 nginx 配置服务器时,我们可能会在 nginx 配置中遇到 $connection_upgrade
变量。
$connection_upgrade 变量默认不可用。 但是,建议在反向代理设置中定义和使用它。
本篇文章将展示如何修复 nginx 与连接升级相关的 unknown variable 错误!
Unknown "connection_upgrade" Variable 错误
我们可能会在(更新之后)使用 nginx -t
检查自己的 nginx 配置时遇到此问题:
$ sudo nginx -t
nginx: [emerg] unknown "connection_upgrade" variable
nginx: configuration file /etc/nginx/nginx.conf test failed
connection_upgrade
变量不是全局 nginx 设置。 然而,我们会在整个 Internet 的教程和代码片段中看到它。 连 nginx 公司都推荐定义和使用 connection_upgrade。 让我们开始修复它吧!
配置 “$connection_upgrade” 变量
连接升级通常与 WebSockets 结合使用。 在 nginx 中,我们可以根据 $http_upgrade
变量将 HTTP 连接升级为 WebSocket 连接。
我们可以使用 map 块在 nginx 中定义连接和 http 升级之间的依赖关系:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
如果 Upgrade 标头设置为 '',此 map 块告诉 nginx 正确设置相关的 Connection 标头来关闭连接。
将 map 块放入 nginx 配置的 http 块中。 nginx 配置的默认文件路径是 /etc/nginx/nginx.conf 。
这是一个使用定义 $connection_upgrade 变量的 map 块的 nginx 配置示例。
/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 … }
保存更新的 nginx 配置文件。 然后,使用 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
我们看到,配置已经生效了。✌️
使用 “$connection_upgrade” 变量
下面这个示例,展示了如何使用新定义的 $connection_upgrade
变量。 它与 proxy 标头结合使用。 在我们的 location 块中设置 proxy 标头,如下所示:
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;
}
}
就是这样,使用 nginx 配置支持 WebSocket 连接!
相关文章
How to Install Nginx on Ubuntu 20.04?
发布时间:2025/04/07 浏览次数:157 分类: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.
How to use Let's Encrypt with Nginx to configure https in Ubuntu 20.04
发布时间:2025/04/07 浏览次数:123 分类:OPERATING SYSTEM
-
Let's Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, enabling encrypted HTTPS on your web server. It simplifies the process by providing a software client, Certbot, which a
Install WordPress with Nginx on Ubuntu 18.04
发布时间:2025/04/07 浏览次数:86 分类: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
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:167 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:132 分类: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
发布时间:2025/03/18 浏览次数:198 分类: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
发布时间:2025/03/18 浏览次数:56 分类: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
发布时间:2025/03/17 浏览次数:195 分类: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
发布时间:2025/03/17 浏览次数:98 分类: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