一、场景
nginx使用集群模式代理多个socket链接,socket链接频繁断开重连
二、具体表现如下
三、nginx代理配置
## socket集群
upstream test_socket {
server 192.168.1.233:9901;
server 192.168.1.243:9901;
}
server {
listen 8600;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
## socket.io
location ~* /socket.io/* {
proxy_pass http://test_socket;
# WebScoket Support
proxy_http_version 1.1;
# webscoekt超时时间,如果没有做心跳之类的动作需要配置
proxy_read_timeout 700s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Origin xxx;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
break;
}
}
四、nginx报错信息
1、nginx错误日志
2023/08/30 11:22:19 [error] 192424#0: *109446 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4Tg3l&sid=75b5dd34-7636-4683-8848-402ba4a5d0d4 HTTP/1.1", upstream: "http://192.168.1.243:9901/socket.io/?EIO=3&transport=polling&t=Of4Tg3l&sid=75b5dd34-7636-4683-8848-402ba4a5d0d4", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:19 [error] 192424#0: *109450 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4Tg3u&sid=75b5dd34-7636-4683-8848-402ba4a5d0d4 HTTP/1.1", upstream: "http://192.168.1.243:9901/socket.io/?EIO=3&transport=polling&t=Of4Tg3u&sid=75b5dd34-7636-4683-8848-402ba4a5d0d4", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:20 [error] 192424#0: *109453 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4TgF_&sid=fe446904-412b-487b-b0ca-fe3b153218c5 HTTP/1.1", upstream: "http://192.168.1.243:9901/socket.io/?EIO=3&transport=polling&t=Of4TgF_&sid=fe446904-412b-487b-b0ca-fe3b153218c5", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:20 [error] 192424#0: *109451 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4TgG7&sid=fe446904-412b-487b-b0ca-fe3b153218c5 HTTP/1.1", upstream: "http://192.168.1.243:9901/socket.io/?EIO=3&transport=polling&t=Of4TgG7&sid=fe446904-412b-487b-b0ca-fe3b153218c5", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:21 [error] 192424#0: *109492 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "GET /socket.io/?EIO=3&transport=polling&t=Of4TgYr&sid=e52704e9-f99c-45df-89c1-1d2424277a77 HTTP/1.1", upstream: "http://192.168.1.243:9901/socket.io/?EIO=3&transport=polling&t=Of4TgYr&sid=e52704e9-f99c-45df-89c1-1d2424277a77", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:22 [error] 192424#0: *109497 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4Tgk1&sid=f5bfb349-17bf-4a81-853c-23c52acf2b35 HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4Tgk1&sid=f5bfb349-17bf-4a81-853c-23c52acf2b35", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:22 [error] 192424#0: *109505 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4Tgk5&sid=f5bfb349-17bf-4a81-853c-23c52acf2b35 HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4Tgk5&sid=f5bfb349-17bf-4a81-853c-23c52acf2b35", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:23 [error] 192424#0: *109517 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "GET /socket.io/?EIO=3&transport=polling&t=Of4Tg_e&sid=b5c9c6a9-f43c-4455-a001-d2dc6f01c39c HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4Tg_e&sid=b5c9c6a9-f43c-4455-a001-d2dc6f01c39c", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:23 [error] 192424#0: *109507 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4Tg_i&sid=b5c9c6a9-f43c-4455-a001-d2dc6f01c39c HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4Tg_i&sid=b5c9c6a9-f43c-4455-a001-d2dc6f01c39c", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:24 [error] 192424#0: *109526 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "GET /socket.io/?EIO=3&transport=polling&t=Of4ThLE&sid=a7896c79-b510-4a37-bf65-8eded388bef3 HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4ThLE&sid=a7896c79-b510-4a37-bf65-8eded388bef3", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:24 [error] 192424#0: *109531 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4ThLJ&sid=a7896c79-b510-4a37-bf65-8eded388bef3 HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4ThLJ&sid=a7896c79-b510-4a37-bf65-8eded388bef3", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:26 [error] 192424#0: *109533 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4Thcj&sid=ee4e2bf1-fc65-4652-99f5-d60e91e88ac3 HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4Thcj&sid=ee4e2bf1-fc65-4652-99f5-d60e91e88ac3", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:26 [error] 192424#0: *109541 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4Thco&sid=ee4e2bf1-fc65-4652-99f5-d60e91e88ac3 HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4Thco&sid=ee4e2bf1-fc65-4652-99f5-d60e91e88ac3", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2023/08/30 11:22:26 [error] 192424#0: *109543 upstream prematurely closed connection while reading upstream, client: 192.168.4.10, server: localhost, request: "POST /socket.io/?EIO=3&transport=polling&t=Of4Thkr&sid=06815ca2-fd5d-4d83-87f3-344dac41a60a HTTP/1.1", upstream: "http://192.168.1.233:9901/socket.io/?EIO=3&transport=polling&t=Of4Thkr&sid=06815ca2-fd5d-4d83-87f3-344dac41a60a", host: "192.168.1.233:8600", referrer: "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1"
2、nginx访问日志
192.168.4.10 - - [30/Aug/2023:14:10:29 +0800] "GET /socket.io/?EIO=3&transport=websocket&sid=44896d85-8569-42c3-adc6-7ce49132f6f0 HTTP/1.1" 101 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:29 +0800] "GET /socket.io/?EIO=3&transport=polling&t=Of549Rf&sid=44896d85-8569-42c3-adc6-7ce49132f6f0 HTTP/1.1" 200 42 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:29 +0800] "POST /socket.io/?EIO=3&transport=polling&t=Of549Rk&sid=44896d85-8569-42c3-adc6-7ce49132f6f0 HTTP/1.1" 500 0 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:29 +0800] "POST /socket.io/?EIO=3&transport=polling&t=Of549Rp&sid=44896d85-8569-42c3-adc6-7ce49132f6f0 HTTP/1.1" 200 39 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:29 +0800] "GET /socket.io/?EIO=3&transport=polling&t=Of549Rk.0&sid=44896d85-8569-42c3-adc6-7ce49132f6f0 HTTP/1.1" 500 0 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:30 +0800] "GET /socket.io/?EIO=3&transport=polling&t=Of549ic HTTP/1.1" 200 147 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:30 +0800] "GET /socket.io/?EIO=3&transport=websocket&sid=1c735e86-00ba-4125-9cbf-c3923feda84e HTTP/1.1" 101 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:30 +0800] "GET /socket.io/?EIO=3&transport=polling&t=Of549ik&sid=1c735e86-00ba-4125-9cbf-c3923feda84e HTTP/1.1" 200 42 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:30 +0800] "POST /socket.io/?EIO=3&transport=polling&t=Of549ip&sid=1c735e86-00ba-4125-9cbf-c3923feda84e HTTP/1.1" 500 0 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:30 +0800] "POST /socket.io/?EIO=3&transport=polling&t=Of549iu&sid=1c735e86-00ba-4125-9cbf-c3923feda84e HTTP/1.1" 500 0 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:31 +0800] "GET /socket.io/?EIO=3&transport=polling&t=Of549vb HTTP/1.1" 200 147 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:31 +0800] "GET /socket.io/?EIO=3&transport=websocket&sid=cb931664-f714-4e01-8c60-d1dd515473a3 HTTP/1.1" 101 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:31 +0800] "GET /socket.io/?EIO=3&transport=polling&t=Of549vj&sid=cb931664-f714-4e01-8c60-d1dd515473a3 HTTP/1.1" 200 42 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:31 +0800] "POST /socket.io/?EIO=3&transport=polling&t=Of549vo&sid=cb931664-f714-4e01-8c60-d1dd515473a3 HTTP/1.1" 500 0 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:31 +0800] "POST /socket.io/?EIO=3&transport=polling&t=Of549vw&sid=cb931664-f714-4e01-8c60-d1dd515473a3 HTTP/1.1" 500 0 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:32 +0800] "GET /socket.io/?EIO=3&transport=polling&t=Of54A3O HTTP/1.1" 200 146 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:32 +0800] "GET /socket.io/?EIO=3&transport=websocket&sid=e9808cde-0f07-4539-9b60-20bf099e12f4 HTTP/1.1" 101 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:32 +0800] "GET /socket.io/?EIO=3&transport=polling&t=Of54A3V&sid=e9808cde-0f07-4539-9b60-20bf099e12f4 HTTP/1.1" 200 42 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.4.10 - - [30/Aug/2023:14:10:32 +0800] "POST /socket.io/?EIO=3&transport=polling&t=Of54A3a&sid=e9808cde-0f07-4539-9b60-20bf099e12f4 HTTP/1.1" 500 0 "http://192.168.1.233:8600/FS_WEB_ASS/cloud/app_websocket.html?originId=1&vdnId=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
五、服务端socket链接日志
2023-08-30 14:28:59.128 - [ERROR] - [nioEventLoopGroup-3-16] - [c.c.socketio.transport.PollingTransport - line:147]: c40dc987-8544-47b5-958b-3952967f1d47 is not registered. Closing connection
2023-08-30 14:28:59.147 - [ERROR] - [nioEventLoopGroup-3-17] - [c.c.socketio.transport.PollingTransport - line:168]: dc0fa35e-35a3-45fd-9a9e-26ba4746b4c8 is not registered. Closing connection
2023-08-30 14:28:59.588 - [INFO ] - [nioEventLoopGroup-3-18] - [com.xxx.socket.hander.SocketService - line:60]: 192.168.1.233:52116 移动客户端接入!
2023-08-30 14:28:59.588 - [INFO ] - [nioEventLoopGroup-3-18] - [com.xxx.socket.interceptor.EventListener - line:40]: 建立连接
2023-08-30 14:28:59.599 - [INFO ] - [nioEventLoopGroup-3-19] - [com.xxx.socket.hander.SocketService - line:68]: 192.168.1.233:52116 移动客户端已断开连接!
2023-08-30 14:28:59.599 - [INFO ] - [nioEventLoopGroup-3-19] - [com.xxx.socket.interceptor.EventListener - line:60]: 关闭连接。。。。。。。。。
2023-08-30 14:28:59.760 - [INFO ] - [nioEventLoopGroup-3-20] - [com.xxx.socket.hander.SocketService - line:60]: 192.168.1.233:52124 移动客户端接入!
2023-08-30 14:28:59.760 - [INFO ] - [nioEventLoopGroup-3-20] - [com.xxx.socket.interceptor.EventListener - line:40]: 建立连接
2023-08-30 14:28:59.767 - [INFO ] - [nioEventLoopGroup-3-21] - [com.xxx.socket.hander.SocketService - line:68]: 192.168.1.233:52124 移动客户端已断开连接!
2023-08-30 14:28:59.767 - [INFO ] - [nioEventLoopGroup-3-21] - [com.xxx.socket.interceptor.EventListener - line:60]: 关闭连接。。。。。。。。。
2023-08-30 14:29:00.257 - [INFO ] - [nioEventLoopGroup-3-22] - [com.xxx.socket.hander.SocketService - line:60]: 192.168.1.233:52132 移动客户端接入!
2023-08-30 14:29:00.258 - [INFO ] - [nioEventLoopGroup-3-22] - [com.xxx.socket.interceptor.EventListener - line:40]: 建立连接
2023-08-30 14:29:00.263 - [INFO ] - [nioEventLoopGroup-3-23] - [com.xxx.socket.hander.SocketService - line:68]: 192.168.1.233:52132 移动客户端已断开连接!
2023-08-30 14:29:00.263 - [INFO ] - [nioEventLoopGroup-3-23] - [com.xxx.socket.interceptor.EventListener - line:60]: 关闭连接。。。。。。。。。
2023-08-30 14:29:00.534 - [ERROR] - [nioEventLoopGroup-3-24] - [c.c.socketio.transport.PollingTransport - line:168]: 1a348191-8b2a-42fc-b08e-552e5029507b is not registered. Closing connection
2023-08-30 14:29:00.538 - [ERROR] - [nioEventLoopGroup-3-25] - [c.c.socketio.transport.PollingTransport - line:147]: 1a348191-8b2a-42fc-b08e-552e5029507b is not registered. Closing connection
2023-08-30 14:29:00.742 - [ERROR] - [nioEventLoopGroup-3-26] - [c.c.socketio.transport.PollingTransport - line:168]: 7740ce73-8509-4233-a735-3c436a144d0e is not registered. Closing connection
2023-08-30 14:29:00.747 - [ERROR] - [nioEventLoopGroup-3-27] - [c.c.socketio.transport.PollingTransport - line:147]: 7740ce73-8509-4233-a735-3c436a144d0e is not registered. Closing connection
2023-08-30 14:29:02.047 - [INFO ] - [nioEventLoopGroup-3-28] - [com.xxx.socket.hander.SocketService - line:60]: 192.168.1.233:52156 移动客户端接入!
2023-08-30 14:29:02.047 - [INFO ] - [nioEventLoopGroup-3-28] - [com.xxx.socket.interceptor.EventListener - line:40]: 建立连接
2023-08-30 14:29:02.049 - [INFO ] - [nioEventLoopGroup-3-29] - [com.xxx.socket.hander.SocketService - line:60]: 192.168.1.233:52160 移动客户端接入!
2023-08-30 14:29:02.049 - [INFO ] - [nioEventLoopGroup-3-29] - [com.xxx.socket.interceptor.EventListener - line:40]: 建立连接
从日志上可以看到,断开重连频率非常高,每隔几十毫秒就会链接一次
六、原因
是nginx集群的负载均衡策略有问题,因为没有在test_socket集群
指定策略,所以nginx默认使用的是轮询策略
那为什么轮询策略会导致socket不断重连呢?
是因为WebSocket协议握手的过程中不是一步而就的,它分了几个步骤,如果说上一次是与服务器1握手,下一次是与服务器2握手,那么肯定没法正常完成握手流程,连接也就没法正常建立了
七、解决
既然原因是因为socket链接没办法与固定的服务器进行通讯,那么只要解决这个问题就可以了
1、为socket集群指定负载均衡策略:ip_hash
## socket集群
upstream test_socket {
# ip_hash:根据IP地址决定具体访问的服务器
ip_hash;
server 192.168.1.233:9901;
server 192.168.1.243:9901;
}
2、重启nginx