https://blog.csdn.net/huangbaokang/article/details/79917023 这篇博客只是操作系统级别的“进程最大可打开文件”的限制。
在nginx.conf文件中加入worker_rlimit_nofile
配置
未加这个配置的时候,启动好了nginx服务,我们可以查看到当前nginx进程最大可打开的文件数。如下:
[root@localhost ~]# ps -ef|grep nginx
root 927 1 0 15:32 ? 00:00:00 nginx: master process /root/hbk/nginx2/sbin/nginx
root 928 927 0 15:32 ? 00:00:00 nginx: worker process
root 929 927 0 15:32 ? 00:00:00 nginx: worker process
root 930 927 0 15:32 ? 00:00:00 nginx: worker process
root 931 927 0 15:32 ? 00:00:00 nginx: worker process
root 1065 1049 0 15:34 pts/0 00:00:00 grep --color=auto nginx
在我这里,有1个master process和4个worker process,随便找一个进程查看:
[root@localhost ~]# cat /proc/928/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 3818 3818 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 3818 3818 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
可以看出,未配置worker_rlimit_nofile
,在我的linux下open files 显示的是
Max open files 1024 4096
配置改写
user root;
worker_processes 4;
worker_rlimit_nofile 65535;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65535;
}
主要是进行了两处改写,加上worker_rlimit_nofile 65535;
和设置worker_connections 65535;
重启nginx服务之后,再次验证。
[root@localhost nginx2]# sbin/nginx -s reload
[root@localhost nginx2]# ps -ef|grep nginx
root 927 1 0 15:32 ? 00:00:00 nginx: master process /root/hbk/nginx2/sbin/nginx
root 1098 927 2 15:52 ? 00:00:00 nginx: worker process
root 1099 927 2 15:52 ? 00:00:00 nginx: worker process
root 1100 927 2 15:52 ? 00:00:00 nginx: worker process
root 1101 927 2 15:52 ? 00:00:00 nginx: worker process
root 1103 1049 0 15:52 pts/0 00:00:00 grep --color=auto nginx
[root@localhost nginx2]# cat /proc/1100/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 3818 3818 processes
Max open files 65535 65535 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 3818 3818 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 变成了65535了。温馨提示,在生产环境下,一定要确保Nginx工作进程的配置信息是经过优化的,否则Nginx对并发请求的处理能力将会大打折扣。