一、简介
通常来说,一个正常的 Nginx Linux 服务器可以达到 500000 – 600000 次/秒 的请求处理性能,如果Nginx服务器经过优化的话,则可以稳定地达到 100000 次/秒 的处理性能。
这里用的是vmware虚拟机环境,配置是6核、4G内存的配置、CentOS 7 系统。
文章参考以下安装方式的Nginx 1.18.0
https://blog.csdn.net/qq_32415063/article/details/105888217
二、解决问题点
1、nginx接受的tcp连接多,能否建立起来?
2、nginx响应过程,要打开许多文件,能否打开?
三、添加nginx状态信息查看
打开nginx的状态连接信息:
[root@localhost ~]#vi /appdata/nginx/conf/nginx.conf
在server里添加如下信息
location /status
{
stub_status on;
access_log off;
}
如下图
重载配置
[root@localhost ~]#systemctl reload nginx
访问地址 http://机器ip地址/status,出现如下信息
四、优化步骤
1、Nginx的CPU配置
获取cpu核心数
[root@localhost conf]# grep processor /proc/cpuinfo | wc -l
比如6核配置:
worker_processes 6;
worker_cpu_affinity 0001 0010 0100 1000 1001 10010;
比如8核配置:
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 0000100000010000 00100000 01000000 10000000;
我这里是六核,修改nginx配置
[root@localhost conf]# vi /appdata/nginx/conf/nginx.conf
如图所示:
http模块新增以下参数
sendfile on;
tcp_nopush on;
client_max_body_size 1024m;
client_body_buffer_size 10m;
client_header_buffer_size 10m;
proxy_buffers 4 128k;
proxy_busy_buffers_size 128k;
open_file_cache max=102400 inactive=20s;
#这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长
时间文件没被请求后删除缓存。
open_file_cache_valid 30s;
keepalive_timeout 60;
2、Linux系统参数修改
[root@localhost conf]# vi /etc/sysctl.conf
配置如下
vm.swappiness=0
#增加tcp支持的队列数
net.ipv4.tcp_max_syn_backlog = 262144
#减少断开连接时 ,资源回收
net.ipv4.tcp_max_tw_buckets = 8000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 10
#改变本地的端口范围
net.ipv4.ip_local_port_range = 1024 65535
#对于只在本地使用的数据库服务器
net.ipv4.tcp_fin_timeout = 1
#端口监听队列
net.core.somaxconn=65535
#接受数据的速率
net.core.netdev_max_backlog=65535
net.core.wmem_default=87380
net.core.wmem_max=16777216
net.core.rmem_default=87380
net.core.rmem_max=16777216
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_max_orphans = 262144
生效参数
[root@localhost conf]# sysctl -p
系统连接数的优化
[root@localhost conf]# vi /etc/security/limits.conf
最后添加如下
* soft nofile 65535
* hard nofile 65535
* soft noproc 65535
* hard noproc 65535
设置连接数最大
[root@localhost conf]# ulimit -n 65536
压测10万并发 (Apache ab 测试工具)安装方式参考链接:
https://blog.csdn.net/qq_32415063/article/details/105896406
[root@localhost ~]# ab -n 200000 -c 5000 http://192.168.23.129:80/index.html
结果如下
Server Software: nginx/1.18.0
Server Hostname: 192.168.23.129
Server Port: 80
Document Path: /index.html
Document Length: 612 bytes
Concurrency Level: 5000
Time taken for tests: 82.459 seconds
Complete requests: 200000
Failed requests: 0
Write errors: 0
Total transferred: 168999155 bytes
HTML transferred: 122399388 bytes
Requests per second: 2425.44 [#/sec] (mean)
Time per request: 2061.482 [ms] (mean)
Time per request: 0.412 [ms] (mean, across all concurrent requests)
Transfer rate: 2001.45 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 875 2739.5 78 31732
Processing: 3 209 722.7 77 61598
Waiting: 0 206 709.2 76 36979
Total: 3 1084 2849.8 180 68689
Percentage of the requests served within a certain time (ms)
50% 180
66% 458
75% 1130
80% 1169
90% 3102
95% 3552
98% 7512
99% 15209
100% 68689 (longest request)