Nginx配置WebSocket
# 一、对wss与nginx代理wss的理解
- wss协议实际是websocket+SSL,就是在websocket协议上加入SSL层,类似https(http+SSL)。
- 利用nginx代理wss【通讯原理及流程】 客户端发起wss连接连到nginx nginx将wss协议的数据转换成ws协议数据并转发到Workerman的websocket协议端口 Workerman收到数据后做业务逻辑处理 Workerman给客户端发送消息时,则是相反的过程,数据经过nginx/转换成wss协议然后发给客户端
# 二、Nginx 支持websocket的配置
server {
listen 80;
server_name 域名;
access_log /var/log/nginx/test_websocket.access.log main;
location / {
proxy_pass http://127.0.0.1:8080/; # 代理转发地址
proxy_http_version 1.1;
proxy_read_timeout 3600s; # 超时设置
# 启用支持websocket连接
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /upload { # 静态资源地址
root /mnt/resources;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
重要的是这两行,它表明是websocket连接进入的时候,进行一个连接升级将http连接变成websocket的连接。
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
1
2
3
2
3
proxy_read_timeout; 表明连接成功以后等待服务器响应的时候,如果不配置默认为60s;
proxy_http_version 1.1; 表明使用http版本为1.1
上次更新: 2024/02/20, 17:31:36
- 01
- linux 在没有 sudo 权限下安装 Ollama 框架12-23
- 02
- Express 与 vue3 使用 sse 实现消息推送(长连接)12-20