Welcome! Log In Create A New Profile

Advanced

Nginx proxy websocket cannot release memory

Posted by Jictinker 
Nginx proxy websocket cannot release memory
February 11, 2022 04:19AM
why client connection closed, can not release memory?

nginx version: nginx/1.21.3

1. memory info (no connections)
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b7c1f1b847c8 nginx_webscoket 0.00% 338.9MiB / 1GiB 33.10% 586B / 42B 0B / 0B 5

2. memory info (200 websocket connections active)
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b7c1f1b847c8 nginx_webscoket 0.00% 342.4MiB / 1GiB 33.44% 255kB / 268kB 0B / 0B 5

# curl http://localhost:21170/status
Active connections: 201
server accepts handled requests
201 201 201
Reading: 0 Writing: 201 Waiting: 0

/# ps aux|grep nginx
root 1 0.0 0.0 18860 3532 ? Ss 09:05 0:00 nginx: master process nginx -g daemon off;
nginx 24 0.0 0.5 105276 87584 ? S 09:05 0:00 nginx: worker process
nginx 25 0.0 0.5 105276 87584 ? S 09:05 0:00 nginx: worker process
nginx 26 0.0 0.5 105276 87584 ? S 09:05 0:00 nginx: worker process
nginx 27 0.0 0.5 108840 91488 ? S 09:05 0:00 nginx: worker process
root 35 0.0 0.0 3072 712 pts/0 S+ 09:08 0:00 grep nginx

3.client websocket connections closed(browser closed, memory can not release)

# curl http://localhost:21170/status
Active connections: 1
server accepts handled requests
203 203 203
Reading: 0 Writing: 1 Waiting: 0

ps aux|grep nginx
root 1 0.0 0.0 18860 3532 ? Ss 09:05 0:00 nginx: master process nginx -g daemon off;
nginx 24 0.0 0.5 105276 87584 ? S 09:05 0:00 nginx: worker process
nginx 25 0.0 0.5 105276 87584 ? S 09:05 0:00 nginx: worker process
nginx 26 0.0 0.5 105276 87584 ? S 09:05 0:00 nginx: worker process
nginx 27 0.0 0.5 108840 91488 ? S 09:05 0:00 nginx: worker process
root 43 0.0 0.0 3072 716 pts/0 S+ 09:10 0:00 grep nginx

4. gdb dump memory (with header cache)
(gdb) dump memory memory.dump 0x56460c87f000 0x56460cbfa000

# strings -n 10 memory.dump |more
datewebsocket-acceptonns
hostconnectionpragmacache-controluser-agentupgradeoriginsec-websocket-versionaccept-encodingaccept-languagedntsec-gpcsec-websocket-keysec-websocket-extensions
GET /ws/test16445704033286 HTTP/1.1
Host: 10.5.2.210:11170
X-Real-IP: 10.5.2.161
X-Forwarded-For: 10.5.2.161
Upgrade: websocket
Connection: upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.43
Origin: http://10.5.2.212:18071
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate
......

5.nginx conf
http
{
include mime.types;
default_type application/octet-stream;
#general options
server_names_hash_bucket_size 128;
# client_header_buffer_size 32k;
client_header_buffer_size 1k;
#large_client_header_buffers 4 128k;
large_client_header_buffers 4 8k;
client_max_body_size 3000m;
proxy_ignore_client_abort on;

#client_header_timeout 12m;
#client_body_timeout 12m;
client_header_timeout 20m;
client_body_timeout 20m;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_requests 320;
#keepalive_timeout 720;
keepalive_timeout 1200;
max_ranges 1;

#open_file_cache max=409600 inactive=20s;
#open_file_cache_valid 30s;
#open_file_cache_min_uses 1;

gzip off;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";

server_tokens off;
#log format
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr"';
# access_log /data/nginx/logs/access.log main;

server {
listen 21170;
server_name 10.5.2.210;

proxy_buffering off;
proxy_buffer_size 2k;
proxy_buffers 8 4k;
#proxy_busy_buffer_size 32k;


location / {
proxy_set_header Host $host:11170;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.5.2.211:11170;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 1200s;
proxy_read_timeout 1200s;
proxy_send_timeout 1200s;
}

location /status {
stub_status on;
auth_basic "nginxstatus";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

#access_log /data/nginx/logs/proxy_access.log main;
access_log /var/log/nginx/proxy_access.log.gz main buffer=8192k gzip=2 flush=20s;
#access_log off;
}

}

6.client code (websocket.html)
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript"type="text/javascript">
var wsUri ="ws://10.5.2.210:21170/ws/eg_id_xgs_"+new Date().getTime();

function init() {
for(int i =0; i < 200; i++){
testWebSocket();
}

}

var heartCheck = {
timeout: 30000,//60ms
timeoutObj: null,
reset: function(){
clearTimeout(this.timeoutObj);    
this.start();
},
start: function(){
this.timeoutObj = setTimeout(function(){
ws.send("HeartBeat");
}, this.timeout)
}
}


function testWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) {
heartCheck.start();
};
websocket.onclose = function(evt) {

};
websocket.onmessage = function(evt) {
heartCheck.reset();
};
websocket.onerror = function(evt) {

};
}
init();
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>
</html>
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 274
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready