nginx 性能优化教程
2022年5月31日大约 1 分钟约 381 字
性能优化
ab压力测试工具
安装:yum install httpd-tools
使用:
ab -n 2000 -c 2 http://127.0.0.1
# -n 总请求数
# -c 并发数
# -k 是否开启长连接
修改文件句柄
对用户设置句柄
#>>> vim /etc/security/limits.conf
root soft nofile 65535 # 对root用户的软限制,超过只会提醒
root hard nofile 65535 # 对root用户的硬限制
* soft nofile 25535 # 所有用户全局限制
* hard nofile 25535
对nginx修改文件句柄
#>>> vim /etc/nginx/nginx.conf
worker_rlimit_nofile 35535;
CPU的亲和
把进程绑定到CPU上,减少进程切换的性能损耗
# 查看当前Cpu个数
cat /proc/cpuinfo | grep "physical id" |sort|uniq|wc -l
# 查看每个CPU的核心数
cat /proc/cpuinfo | grep "cpu cores" | uniq
# top命令后按1可以显示所有核心
#>>> vim /etc/nginx/nginx.conf
worker_processes 4; # 与cpu核心总数一致
#worker_cpu_affinity 0001 0010 0100 1000; # 四个进程对应4个核心,位置代表不同的进程,数字代表cpu核心编号
worker_cpu_affinity auto; # 自动配置cpu亲和
# ps -eo pid,args,psr | grep [n]ginx # 查看配置的效果
通用性能配置
# 事件驱动器
events {
use epoll; # linux用epoll
worker_connections 10240; # 每个worker进程能处理多少连接,默认1024个
}
http {
charset utf-8; # 字符集
# 打开sendfile
sendfile on;
# tcp_nopush on; # 作为静态资源服务器建议打开
# tcp_nodeny on; # 做为动态服务和keepalive打开的情况可以打开
keepalive_timeout 65;
# 打开压缩
gzip on;
gzip_disable "MSIE [1-6]\."; # 对IE6不压缩,兼容
gzip_http_version 1.1;
}