Nginx合集-性能优化(nginx性能调优)

Nginx合集-性能优化(nginx性能调优)

精选文章moguli202025-05-08 14:06:5211A+A-


1、工具

ab:Apache Benchmark(简称ab) 是Apache安装包中的压力测试工具 ,本案使用ab进行性能测试。

2、ab安装

需要安装依赖apr-util包

 yum install apr-util 

yumdownload命令安装:

 yum install yum-utils

创建目录

mkdir -p /appdata/apache/ab && cd /appdata/apache/ab

下载安装包

 yum install yum-utils.noarch
 yumdownloader httpd-tools* 

解压安装包

rpm2cpio httpd-tools*.rpm | cpio -idmv

将./user/bin/ab复制到系统bin下

cp /appdata/apache/ab/usr/bin/ab /usr/bin

3、ab压测

服务器配置

操作系统:CentOS Linux release 7.9.2009 (Core)
cpu:8核
内存:16G
磁盘:90G

压测:20万,无静态资源

ab -n 200000 -c 5000 http://xxx.xxx.xxx.xxx/index.html   # -n 请求次数   -c 并发数

#报告
Server Software:        nginx
Server Hostname:        10.0.53.84
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      5000   #并发数
Time taken for tests:   12.075 seconds    #总请求时长
Complete requests:      200000   #总请求数
Failed requests:        0
Write errors:           0
Total transferred:      204200000 bytes
HTML transferred:       122400000 bytes
Requests per second:    16562.75 [#/sec] (mean)    #QPS 吞吐量
Time per request:       301.882 [ms] (mean)      #用户请求平均等待时间
Time per request:       0.060 [ms] (mean, across all concurrent requests)   #服务器处理每个请求花费的时间
Transfer rate:          16514.23 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  131  35.5    130    1139
Processing:    56  168  29.5    161     276
Waiting:        0  125  31.4    110     204
Total:        162  299  36.2    294    1301

Percentage of the requests served within a certain time (ms)
  50%    294
  66%    309
  75%    312
  80%    313
  90%    317
  95%    323
  98%    332
  99%    337
 100%   1301 (longest request)

压测:100万,无静态资源

ab -n 1000000 -c 5000 http://10.0.53.84/index.html


#报告
Server Software:        nginx
Server Hostname:        10.0.53.84
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      5000
Time taken for tests:   59.294 seconds
Complete requests:      1000000
Failed requests:        0
Write errors:           0
Total transferred:      1021000000 bytes
HTML transferred:       612000000 bytes
Requests per second:    16865.11 [#/sec] (mean)
Time per request:       296.470 [ms] (mean)
Time per request:       0.059 [ms] (mean, across all concurrent requests)
Transfer rate:          16815.70 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  131  73.2    129    3136
Processing:    52  165  25.7    156     227
Waiting:        1  123  29.2    106     195
Total:        171  296  74.4    288    3305

Percentage of the requests served within a certain time (ms)
  50%    288
  66%    308
  75%    310
  80%    311
  90%    313
  95%    315
  98%    317
  99%    320
 100%   3305 (longest request)

压测:20万,1M内的静态资源(一般前端包不大于1Mb)

ab -n 200000 -c 5000 http://10.0.53.84/1.jpg

#报告

Server Software:        nginx
Server Hostname:        10.0.53.84
Server Port:            80

Document Path:          /1.jpg
Document Length:        999398 bytes    #静态资源文件大小

Concurrency Level:      5000 #并发数
Time taken for tests:   85.020 seconds  #总请求时长
Complete requests:      200000  #总请求数
Failed requests:        0
Write errors:           0
Total transferred:      199962600000 bytes
HTML transferred:       199879600000 bytes
Requests per second:    2352.39 [#/sec] (mean)   #QPS 吞吐量
Time per request:       2125.498 [ms] (mean)   #用户请求平均等待时间
Time per request:       0.425 [ms] (mean, across all concurrent requests)  #服务器处理每个请求花费的时间
Transfer rate:          2296826.54 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   36  16.2     34     174
Processing:   937 2081 132.4   2094    2711
Waiting:        0   44  57.0     37     676
Total:       1086 2117 128.5   2129    2791

Percentage of the requests served within a certain time (ms)
  50%   2129
  66%   2139
  75%   2146
  80%   2150
  90%   2166
  95%   2174
  98%   2186
  99%   2398
 100%   2791 (longest request)

服务器性能截图

编辑

结论:根据连云港nginx请求统计在高峰时刻1分钟内达到6万次请求时候系统保持稳定,超过8万次请求时候接口会返回系统异常情况。优化配置系统内核和nginx配置文件之后,进行nginx入口并发压测,读取1Mb的文件,每秒可请求2352.39次,预估1分钟可达到10万请求,能够满足前端的高峰性能要求。

4、优化记录

优化nginx的配置文件中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;

优化linux内核参数

  #增加tcp支持的队列数
	net.ipv4.tcp_max_syn_backlog = 262144
	#减少断开连接时 ,资源回收
	net.ipv4.tcp_max_tw_buckets = 8000
	# 表示开启重用。
	# 允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭; 
	net.ipv4.tcp_tw_reuse = 1
	net.ipv4.tcp_tw_recycle = 1
	# 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。针对nginx
	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=262144
	net.core.wmem_default=8388608
	net.core.wmem_max=16777216
	net.core.rmem_default=8388608
	net.core.rmem_max=16777216
	# 开启SYN Cookies。
	#  当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,
	#  默认为0,表示关闭;
	net.ipv4.tcp_syncookies = 1
	net.ipv4.tcp_max_orphans = 3276800
  #  系统默认值为”65536”,系统会主动丢掉新连接包,直到连接小于此设置值才会恢复。
  net.nf_conntrack_max = 655360
  #  单位s,系统默认值为”432000”,代表nf_conntrack的TCP连接记录时间默认是5天,致使nf_conntrack的值减不下来,丢包持续时间长。
  net.netfilter.nf_conntrack_tcp_timeout_established = 1200

点击这里复制本文地址 以上内容由莫古技术网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

莫古技术网 © All Rights Reserved.  滇ICP备2024046894号-2