nginx代理socket链接集群后,频繁断开重连

这篇具有很好参考价值的文章主要介绍了nginx代理socket链接集群后,频繁断开重连。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


一、场景

nginx使用集群模式代理多个socket链接,socket链接频繁断开重连


二、具体表现如下

nginx代理socket链接集群后,频繁断开重连,Java开发操作,Nginx,nginx,运维,websocket


三、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文章来源地址https://www.toymoban.com/news/detail-708227.html

到了这里,关于nginx代理socket链接集群后,频繁断开重连的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • nginx代理webSocket链接响应403

    使用nginx代理webSocket链接,nginx响应403 1、nginx访问日志响应403 2、浏览器链接失败 是 proxy_set_header Origin xxx 导致的 Origin xxx的具体作用不明确,有知道的欢迎留言 proxy_set_header Origin xxx 改为 proxy_set_header Origin \\\"\\\" 最终配置如下 补充:使用 WSS 访问时,如果不是 443 端口,也会出现

    2024年02月11日
    浏览(41)
  • [Android][WIFI]手机作AP,关闭移动网络后,STA端断开重连问题分析

    测试平台 Android版本:Android P(9.0) 复现步骤 准备两台移动设备,一台作为AP,一台作为STA; 作为AP的设备具备移动网络上网,热点网络分享能力; 打开作为AP的设备的移动网络流量开关,再打开热点; 作为STA的设备接入该AP,并等待其通路判断完成,确保Internet网络访问能力正

    2024年02月10日
    浏览(71)
  • Nginx 配置反向代理不生效(代理到nacos集群)

    环境:centos8 相信各位安装都不成问题,反向代理配置也都能安装网上资料配置完成,但问题就出在配置好后启动Nginx,访问默认端口能成功: 但是访问自己配置的反向代理就会失败(如以下这种情况等): 我所遇到的问题出处:在我自建的/usr/local/nginx文件夹下下载了nginx压

    2024年02月16日
    浏览(33)
  • Nginx反向代理配置+负载均衡集群部署

    什么是代理 正向代理既是通过代理服务器访问外网资源,而反向代理与之相反是将局域网的资源通过反向代理服务器提供给互联网用户浏览和使用等。 实验环境图 流量过程 当客户端发起请求后通过Internet (本实验为NAT网卡)来到代理服务器上,代理服务器经算法选择后调用

    2024年02月13日
    浏览(41)
  • 宝塔nginx配置SpringBoot服务集群代理

    现有一个springboot服务需要部署成集群,通过nginx负载均衡进行访问,其中这个springboot服务内置了MQTT服务、HTTP服务、TCP服务。 MQTT服务开放了1889端口 HTTP服务开放了8891端口 HTTP服务开放了8893端口 TCP服务开放了8893端口 该服务在宝塔部署启动后开启的端口 宝塔nginx的nginx.conf位置

    2024年02月20日
    浏览(42)
  • Nacos基础(3)——nacos+nginx & 集群的配置和启动 & 端口开放 & nginx反向代理nacos集群

    1.nacos集群启动的方式,修改cluster.conf集群的配置,多个nacos; 2.修改启动配置文件application.properties,多个nacos分别以集群方式启动; 3.nginx反向代理多个nacos,配置代理tcp,以及http; 启动8848, 8858, 8868三个服务器 vim cluster.conf配置文件,配置多个nacos服务端口 同步两个的配置

    2024年02月02日
    浏览(64)
  • Nginx+Tomcat负载均衡(反向代理)、动静分离集群

    Nginx:正向代理(知道目标服务器) 反向代理(不知道目标服务器) Nginx配置反向代理主要参数 upstream服务池名{}(F5(硬件)和nginx反向代理原理一样) 配置后端服务池,以提供响应数据 proxy_pass http://服务池名 配置将访问请求转发给后端服务器池的服务器处理 定义:将工作任务

    2024年02月13日
    浏览(43)
  • java socket Server TCP服务端向指定客户端发送消息;可查看、断开指定连接的客户端;以及设置客户端最大可连接数量。

    首先需要知道java里如何创建一个Socket服务器端。 提示:注意server.accept()方法调用会阻塞,只有新的客户端连接后才返回一个新的socket对象。如果一直未连接那么会一直处于阻塞状态 了解了如何创建一个socket服务器端后。那么如何实现给指定的连接客户端发送消息呢?首先我

    2024年02月11日
    浏览(73)
  • 保姆级微信小程序对接蓝牙设备教程。微信小程序发送不同蓝牙指令(定时发送,断开重连,判断是否有蓝牙权限等)

    本文是一个完整的对接设备,发送不同指令监听不同返回的完整示例,可根据实际项目按需更改。 注: app.showModal 为在app.js中封装的showModal方法, then(()={}) 代表用户点击 confirm ,可用 wx.showModal 代替。 公用方法 请求设备列表 1. 判断是否有蓝牙权限 2. 初始化蓝牙 wx.openBluet

    2024年03月20日
    浏览(65)
  • web集群学习:基于nginx的反向代理和负载均衡

    目录 一,反向代理 1,环境准备 2,配置代理服务器 3,在物理机上一管理员身份打开文本编辑器,编辑C:WindowsSystem32driversetc目录下的hosts文件 4,访问测试 5,查看日志,并记录客户端真实ip地址 二,负载均衡 1,upstream模块 2,实现配置简单的负载均衡 3,实现backup功能 4,

    2024年02月13日
    浏览(86)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包