优化前使用ab高并发工具测试
[root@web1 ~]# yum -y install httpd-tools
[root@web1 ~]# rpm -qa httpd-tools
httpd-tools-2.4.6-97.el7.centos.x86_64
[root@web1 ~]# ab -c 100 -n 100 http://192.168.2.100/ -c代表总人数,-n代表总访问量
[root@web1 ~]# ab -c 2000 -n 2000 http://192.168.2.100/
socket: Too many open files (24) #提示打开文件数量过多
修改Nginx配置文件,增加并发量cpu处理器图形模式查看,改成2个
[root@web1 ~]# lscpu
[root@web1 ~ ]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes 2; #与CPU核心数量一致
events {
worker_connections 50000; #每个worker最大并发连接数
}
[root@web1 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@web1 ~]# ab -c 2000 -n 2000 http://192.168.2.100/ #再次测试,依旧报错
优化Linux内核参数(最大文件数量)
[root@web1 ~]# ulimit -a #查看所有属性值
open files (-n) 1024 #打开文件数量
[root@web1 ~]# ulimit -n 100000 #更改打开的文件数量,修改内核限制(临时)
[root@web1 ~]# ab -c 2000 -n 2000 http://192.168.2.100/
永久修改
[root@web1 ~ ]# vim /etc/security/limits.conf
.. ..
* soft nofile 100000 #软限制
* hard nofile 100000 #硬限制,nofile用户最大文件打开数量
#该配置文件分4列,分别如下:
用户或组 硬限制或软限制 需要限制的项目 限制的值