nginx部署时http接口正常,ws接口404

可以这么配置

bash 复制代码
map $http_upgrade $connection_upgrade {
          default upgrade;
          '' close;
}


upstream wsbackend{
         server ip1:port1;
         server ip2:port2;
         keepalive 1000;
}

server {
       listen 20038;
       location /{ 
            proxy_http_version 1.1;
            proxy_pass http://wsbackend;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_read_timeout 3600s;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
       }
}

首先:

bash 复制代码
map $http_upgrade $connection_upgrade {
          default upgrade;
          '' close;
}

表示的是:

如果 http_upgrade 不为 '' (空),则 connection_upgrade 为 upgrade 。

如果 http_upgrade 为 '' (空),则 connection_upgrade 为 close。

其次:

bash 复制代码
upstream wsbackend{
        server ip1:port1;
        server ip2:port2;
        keepalive 1000;
}

表示的是 nginx负载均衡:

两台服务器 (ip1:port1)和(ip2:port2) 。

keepalive 1000 表示的是每个nginx进程中上游服务器保持的空闲连接,当空闲连接过多时,会关闭最少使用的空闲连接.当然,这不是限制连接总数的,可以想象成空闲连接池的大小,设置的值应该是上游服务器能够承受的。

bash 复制代码
server {
       listen 20038;
       location /{
            proxy_http_version 1.1;
            proxy_pass http://wsbackend;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_read_timeout 3600s;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
       }
}

表示的是监听的服务器的配置

listen 8086 表示 nginx 监听的端口

locations / 表示监听的路径(/表示所有路径,通用匹配,相当于default)

proxt_http_version 1.1 表示反向代理发送的HTTP协议的版本是1.1,HTTP1.1支持长连接

proxy_pass http://wsbackend; 表示反向代理的uri,这里可以使用负载均衡变量

proxy_redirect off; 表示不要替换路径,其实这里如果是/则有没有都没关系,因为default也是将路径替换到proxy_pass的后边

proxy_set_header Host host; 表示传递时请求头不变, host是nginx内置变量,表示的是当前的请求头,proxy_set_header表示设置请求头

proxy_set_header X-Real-IP $remote_addr; 表示传递时来源的ip还是现在的客户端的ip

proxy_read_timeout 3600s; 表的两次请求之间的间隔超过 3600s 后才关闭这个连接,默认的60s,自动关闭的元凶

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 表示X-Forwarded-For头不发生改变

proxy_set_header Upgrade $http_upgrade; 表示设置Upgrade不变

proxy_set_header Connection connection_upgrade; 表示如果 http_upgrade为upgrade,则请求为upgrade(websocket),如果不是,就关闭连接

若是实际使用的websocket地址后面还有路径,比如实际地址是这样的,访问地址是 http://www.a.com,实际会转发给ws://127.0.0.1:8094/ws

Nginx如何支持WebSocket

WebSocket 和HTTP虽然是不同协议,但是两者"握手"方式兼容。通过HTTP升级机制,使用HTTP的Upgrade和Connection协议头的方式可以将连接从HTTP升级为WebSocket

以下是重点:

  1. map是根据客户端请求中 http_upgrade 的值来构造改变 connection_upgrade 的值,即根据变量 http_upgrade 的值和{} 里规则创建新的变量 connection_upgrade并赋值。

  2. HTTP的Upgrade协议头机制用于将连接从HTTP连接升级到WebSocket连接,Upgrade机制使用了Upgrade协议头和Connection协议头;为了让Nginx可以将来自客户端的Upgrade请求发送到后端服务器,Upgrade和Connection的头信息必须被显式的设置。如上图代码中第三个红圈。

相关推荐
韭菜炒大葱1 天前
前端经典面试题:从 URL 输入到页面展示,中间经历了什么?
前端·http·面试
顺风尿一寸1 天前
Nginx源码分析:变量系统的设计与请求生命周期中的日志记录
nginx
爱吃橘子橙子柚子1 天前
3CPU性能排查总结(超详细)【Linux性能优化】
运维·cpu
舒一笑3 天前
程序员效率神器:一文掌握 tmux(服务器开发必备工具)
运维·后端·程序员
NineData3 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库
梦想很大很大4 天前
拒绝“盲猜式”调优:在 Go Gin 项目中落地 OpenTelemetry 链路追踪
运维·后端·go
Sinclair4 天前
内网服务器离线安装 Nginx+PHP+MySQL 的方法
运维
叶落阁主4 天前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
十二7404 天前
前端缓存踩坑实录:从版本号管理到自动化构建
前端·javascript·nginx
可观测性用观测云5 天前
云原生网关 Ingress-Nginx 链路追踪实战:OpenTelemetry 采集与观测云集成方案
nginx·kubernetes