系统 Centos 7.3
然后查看程序限制,主要运行了两个程序,包括 nginx 和 一个 python 脚本,以其中一个为例:
先查看程序 pid:
ps -elf | grep nginx
得到结果:
5 S root 1140 1 0 80 0 - 6072 sigsus 2019 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 S deployer 23158 1140 0 80 0 - 56251 ep_pol 14:54 ? 00:00:00 nginx: worker process
0 S deployer 29692 29649 0 80 0 - 28164 pipe_w 18:43 pts/3 00:00:00 grep --color=auto nginx
其中 worker process
的 23158
就是 nginx 的 pid,接着查看程序的句柄限制:
cat /proc/23158/limits
得到结果:
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 127963 127963 processes
Max open files 1024 1024 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 127963 127963 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
可以看到 Max open files
值为默认的 1024
,然后进入去 nginx 的系统配置里:
sudo vi /usr/local/nginx/conf/nginx.conf
修改如下内容:
#配置Nginx worker进程最大打开文件数
worker_rlimit_nofile 1024;
events {
worker_connections 1024;
}
将这两个数值都改为 524288
然后重启 nginx:
cd /usr/local/nginx/sbin/
sudo ./nginx -t
sudo ./nginx -s reload
再次查看 nginx 进程的最大句柄数限制已经改为 524288
查看程序连接有没有正确关闭:
cat /proc/23158/net/sockstat
得到的结果如下:(如果 TCP inuse 很大,就很有可能是连接没有正确关闭)
sockets: used 428
TCP: inuse 253 orphan 0 tw 1 alloc 256 mem 1
UDP: inuse 5 mem 1
UDPLITE: inuse 0
RAW: inuse 2
FRAG: inuse 0 memory 0
其中字段解释如下:
sockets: used: 已使用的所有协议套接字总量
TCP: inuse: 正在使用(正在侦听)的TCP套接字数量。其值≤ netstat –lnt | grep ^tcp | wc –l
TCP: orphan: 无主(不属于任何进程)的TCP连接数(无用、待销毁的TCP socket数)
TCP: tw: 等待关闭的TCP连接数。其值等于netstat –ant | grep TIME_WAIT | wc –l
TCP:alloc(allocated): 已分配(已建立、已申请到sk_buff)的TCP套接字数量。其值等于netstat –ant | grep ^tcp | wc –l
TCP:mem: 套接字缓冲区使用量(单位不详。用scp实测,速度在4803.9kB/s时:其值=11,netstat –ant 中相应的22端口的Recv-Q=0,Send-Q≈400)
UDP:inuse: 正在使用的UDP套接字数量
FRAG: 使用的IP段数量