配置 “$connection_upgrade” 变量
连接升级通常与 WebSockets 结合使用。 在 nginx 中,我们可以根据 $http_upgrade 变量将 HTTP 连接升级为 WebSocket 连接。
我们可以使用 map 块在 nginx 中定义连接和 http 升级之间的依赖关系:
vim nginx.conf
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
如果 Upgrade 标头设置为 ‘’,此 map 块告诉 nginx 正确设置相关的 Connection 标头来关闭连接。
将 map 块放入 nginx 配置的 http 块中。 nginx 配置的默认文件路径是 /etc/nginx/nginx.conf 。