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的头信息必须被显式的设置。如上图代码中第三个红圈。

相关推荐
qq_5895681040 分钟前
centos6.8镜像源yum install不成功,无法通过镜像源下载的解决方式
linux·运维·centos
weixin_516023071 小时前
linux下fcitx5拼音的安装
linux·运维·服务器
hunter14502 小时前
Linux 进程与计划任务
linux·运维·服务器
楼田莉子2 小时前
Linux学习之磁盘与Ext系列文件
linux·运维·服务器·c语言·学习
陌上花开缓缓归以2 小时前
linux 怎么模拟系统panic重启
linux·运维·服务器
月白风清江有声3 小时前
vscode使用git
linux·运维·服务器
haluhalu.5 小时前
深入理解Linux线程机制:线程概念,内存管理
java·linux·运维
cui__OaO6 小时前
Linux驱动--驱动编译
linux·运维·服务器
Q16849645156 小时前
红帽Linux-进程、ssh、网络、软件包、文件系统
linux·运维·网络
ℳ₯㎕ddzོꦿ࿐6 小时前
Docker 环境下 Paperless-ngx 中文增强版部署实战
运维·docker·容器