nginx安装以及调优

目录:

1.安装nginx

2.配置nginx

3.调优nginx

4.性能测试

ps:为了方便,文档使用docker容器来操作的。

1.安装nginx

1.1 启动容器、download nginx 以及编译前的修改

启动容器

1 liwangdeMacBook-Air:~ liwang$ docker run -i -t --name nginx_server_01 -v /Users/liwang/docker/nginx_data:/data -p 80:80 centos /bin/bash

将文件copy至容器中

1 liwangdeMacBook-Air:~ liwang$ docker cp Downloads/nginx-1.14.0.tar.gz nginx_server_01:/soft/nginx-1.14.0.tar.gz

解压

1 [root@e46ae471064e soft]# tar xf /soft/nginx-1.14.0.tar.gz -C /soft/

安装之前修改源码屏蔽名称以及版本号

 1 [root@e46ae471064e core]# ls -l "/soft/nginx-1.14.0/src/core/nginx.h"
 2 -rw-r--r-- 1 1001 1001 476 Apr 17 15:22 /soft/nginx-1.14.0/src/core/nginx.h
 3 [root@e46ae471064e core]# 
 4 
 5 ------------------------------------------------------
 6 #define NGINX_VERSION      "0.0.1"
 7 #define NGINX_VER          "HaiYan/" NGINX_VERSION
 8 #define NGINX_VAR          "HaiYan"
 9 ------------------------------------------------------
10 #define NGINX_VERSION      "1.14.0"
11 #define NGINX_VER          "nginx/" NGINX_VERSION
12 #define NGINX_VAR          "NGINX"
13 ------------------------------------------------------

1.2 安装nginx

安装插件

1 [root@e46ae471064e ~]# yum install pcre pcre-devel gcc gcc-c++ make openssl openssl-devel -y

添加用户

1 [root@e46ae471064e nginx-1.14.0]# useradd nginx -s /sbin/nologin -M

安装

1 [root@e46ae471064e nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module  --with-http_stub_status_module --user=nginx --group=nginx
2 [root@e46ae471064e nginx-1.14.0]# make
3 [root@e46ae471064e nginx-1.14.0]# make install

启动nginx,利用curl工具获取服务器信息

 1 [root@e46ae471064e conf]# /usr/local/nginx/sbin/nginx 
 2 [root@e46ae471064e nginx]# curl -I localhost    
 3 HTTP/1.1 200 OK
 4 Server: HaiYan/0.0.1
 5 Date: Tue, 01 May 2018 14:39:56 GMT
 6 Content-Type: text/html
 7 Content-Length: 612
 8 Last-Modified: Tue, 01 May 2018 14:33:26 GMT
 9 Connection: keep-alive
10 ETag: "5ae87ab6-264"
11 Accept-Ranges: bytes
12 
13 [root@e46ae471064e nginx]# 

详细可安装信息可用./configure --help获取

2.配置nginx

1、nginx虚拟主机配置

基于域名的虚拟主机

 1 [root@e46ae471064e conf]# cat nginx.conf | grep -v "#" | grep -v "^$" >> extra/www.conf
 2 修改www.conf效果如下:
 3 [root@e46ae471064e extra]# cat www.conf 
 4     server {
 5         listen       80;
 6         server_name  www.wang-li.top;
 7         location / {
 8             root   html/www;
 9             index  index.html index.htm;
10         }
11         error_page   500 502 503 504  /50x.html;
12         location = /50x.html {
13             root   html;
14         }
15     }
16 [root@e46ae471064e extra]# 

修改nginx.conf 在http区间增加,include extra/*; 这行

 1 [root@e46ae471064e conf]# cat nginx.conf | grep -v "#" | grep -v "^$"
 2 worker_processes  1;
 3 events {
 4     worker_connections  1024;
 5 }
 6 http {
 7     include       mime.types;
 8     default_type  application/octet-stream;
 9     sendfile        on;
10     keepalive_timeout  65;
11     server {
12         listen       80;
13         server_name  localhost;
14         location / {
15             root   html;
16             index  index.html index.htm;
17         }
18         error_page   500 502 503 504  /50x.html;
19         location = /50x.html {
20             root   html;
21         }
22     }
23     include extra/*;
24 }
25 [root@e46ae471064e conf]# 

增加html/www/index.html文件

1 [root@e46ae471064e conf]# ls /usr/local/nginx/html/www/index.html -l
2 -rw-r--r-- 1 root root 613 May  1 14:50 /usr/local/nginx/html/www/index.html
3 [root@e46ae471064e conf]# 

修改hosts文件,访问信息如下

 1 [root@e46ae471064e conf]# cat /etc/hosts | grep www.wang-li.top
 2 127.0.0.1    www.wang-li.top
 3 [root@e46ae471064e conf]# curl -I www.wang-li.top
 4 HTTP/1.1 200 OK
 5 Server: HaiYan/0.0.1
 6 Date: Tue, 01 May 2018 14:54:41 GMT
 7 Content-Type: text/html
 8 Content-Length: 613
 9 Last-Modified: Tue, 01 May 2018 14:50:21 GMT
10 Connection: keep-alive
11 ETag: "5ae87ead-265"
12 Accept-Ranges: bytes
13 
14 [root@e46ae471064e conf]# 

基于端口的虚拟主机

修改www.conf基于端口的访问,如下:

 1 [root@e46ae471064e extra]# cat www.conf 
 2     server {
 3         listen       8081;
 4         server_name  www.wang-li.top;
 5         location / {
 6             root   html/www;
 7             index  index.html index.htm;
 8         }
 9         error_page   500 502 503 504  /50x.html;
10         location = /50x.html {
11             root   html;
12         }
13     }
14 [root@e46ae471064e extra]# 

reload nginx 访问如下

 1 [root@e46ae471064e extra]# /usr/local/nginx/sbin/nginx -t
 2 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
 3 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
 4 [root@e46ae471064e extra]# /usr/local/nginx/sbin/nginx -s reload
 5 [root@e46ae471064e extra]# curl -I www.wang-li.top:8081
 6 HTTP/1.1 200 OK
 7 Server: HaiYan/0.0.1
 8 Date: Tue, 01 May 2018 14:56:48 GMT
 9 Content-Type: text/html
10 Content-Length: 613
11 Last-Modified: Tue, 01 May 2018 14:50:21 GMT
12 Connection: keep-alive
13 ETag: "5ae87ead-265"
14 Accept-Ranges: bytes
15 
16 [root@e46ae471064e extra]# 

基于IP的虚拟主机

获取容器ip

1 liwangdeMacBook-Air:~ liwang$ docker inspect -f {{.NetworkSettings.IPAddress}} nginx_server_01
2 172.17.0.2
3 liwangdeMacBook-Air:~ liwang$ 

修改www.conf如下

 1 [root@e46ae471064e extra]# cat www.conf 
 2     server {
 3         listen       172.17.0.2:8082;
 4         server_name  www.wang-li.top;
 5         location / {
 6             root   html/www;
 7             index  index.html index.htm;
 8         }
 9         error_page   500 502 503 504  /50x.html;
10         location = /50x.html {
11             root   html;
12         }
13     }
14 [root@e46ae471064e extra]# 

reload后访问如下:

 1 [root@e46ae471064e extra]# curl -I 172.17.0.2:8082
 2 HTTP/1.1 200 OK
 3 Server: HaiYan/0.0.1
 4 Date: Tue, 01 May 2018 15:00:44 GMT
 5 Content-Type: text/html
 6 Content-Length: 613
 7 Last-Modified: Tue, 01 May 2018 14:50:21 GMT
 8 Connection: keep-alive
 9 ETag: "5ae87ead-265"
10 Accept-Ranges: bytes
11 
12 [root@e46ae471064e extra]# 

2、nginx stub_status配置

查看系统是否支持此模块

1 [root@e46ae471064e extra]# /usr/local/nginx/sbin/nginx -V                
2 nginx version: HaiYan/0.0.1
3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
4 built with OpenSSL 1.0.2k-fips  26 Jan 2017
5 TLS SNI support enabled
6 configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx
7 [root@e46ae471064e extra]# 

发现有--with-http_stub_status_module就是支持的

配置www.conf如下:

 1 [root@e46ae471064e extra]# cat www.conf 
 2     server {
 3         listen       80;
 4         server_name  www.wang-li.top;
 5         location / {
 6 #            root   html/www;
 7 #            index  index.html index.htm;
 8         stub_status on;
 9         access_log off;
10         }
11     }
12 [root@e46ae471064e extra]# 

reload后访问如下

1 root@e46ae471064e extra]# curl www.wang-li.top
2 Active connections: 3 
3 server accepts handled requests
4  21 21 20 
5 Reading: 0 Writing: 1 Waiting: 2 
6 [root@e46ae471064e extra]# 

三个值分别代表:
active connections:正在处理的连接活动数
21 21 20
第一个表示从启动到现在一共处理了21次请求
第二个表示建立了21次握手
第三个表示总共处理了20次请求
丢包数 = 握手数-连接数 可见,并未丢包

reading: nginx 读取到客户端的header信息数
writing: Nginx 返回给客户端的header信息数
waiting: Nginx 处理完等待下一次请求指令的驻留连接,在开启keep-alive的情况下,这个值等于active - (reading + writing)

3、nginx_location:

作用:根据不同的URI来执行不同的应用
语法:
location [ = | ~ | ~* | ^- ] url {
...
}

语法规则: location [=|~|~*|^~] /uri/ { … }

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~* 开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。

~和~*的区别: ~区分大小写, ~* 不区分大小写

= :为精确配置
/ :为默认配置

案例:
利用location来实现根据URI来访问不同的内容,实现功能:访问www.wang-li.top/bbs/XXX时,能够抓取和www.wang-li.top不同的位置

配置如下

 1 [root@e46ae471064e conf]# sed -n "47,59p" nginx.conf
 2     
 3     location ^~ /bbs/ {
 4         root   html/bbs;
 5         index index.html;
 6 #   
 7     }
 8 
 9         location / {
10             root   html;
11             index  index.html index.htm;
12 #   
13    }
14 [root@e46ae471064e conf]# tree /usr/local/nginx/html/
15 /usr/local/nginx/html/
16 |-- 50x.html
17 |-- bbs
18 |   `-- bbs
19 |       `-- index.html
20 |-- index.html
21 `-- www
22     `-- index.html
23 
24 3 directories, 4 files
25 [root@e46ae471064e conf]#

访问如下:

 1 [root@e46ae471064e conf]# curl localhost    
 2 <!DOCTYPE html>
 3 <html>
 4 <head>
 5 <title>Welcome to nginx!</title>
 6 <style>
 7     body {
 8         width: 35em;
 9         margin: 0 auto;
10         font-family: Tahoma, Verdana, Arial, sans-serif;
11     }
12 </style>
13 </head>
14 <body>
15 <h1>Welcome to nginx!</h1>
16 <p>If you see this page, the nginx web server is successfully installed and
17 working. Further configuration is required.</p>
18 
19 <p>For online documentation and support please refer to
20 <a href="http://nginx.org/">nginx.org</a>.<br/>
21 Commercial support is available at
22 <a href="http://nginx.com/">nginx.com</a>.</p>
23 
24 <p><em>Thank you for using nginx.</em></p>
25 </body>
26 </html>
27 [root@e46ae471064e conf]# curl localhost/bbs/
28 Welcome www.wang-li.top PPS 
29 [root@e46ae471064e conf]# 

4、nginx alias 和 root的区别

基于上诉的nginx location,location 和 alias的差别就在于
root会带入上诉的url路径去判断,而alias则不会,举例:
location ^~ /bbs/ {
root html/bbs;
index index.html;
#
}
这段说明:当我的url中包含bbs时,会去执行location的内容,而内容包含root html/bbs,现在如果输入localhost\bbs\时,系统会去检查本地文件html/bbs/bbs/...这下面的内容
而alias则不会
修改如下:

 1 [root@e46ae471064e bbs]# sed -n "48,58p" /usr/local/nginx/conf/nginx.conf
 2     location ^~ /bbs/ {
 3         alias   html/bbs/;
 4         index index.html;
 5 #   
 6     }
 7 
 8         location / {
 9             root   html;
10             index  index.html index.htm;
11 #   
12    }
13 [root@e46ae471064e bbs]# cat /usr/local/nginx/html/bbs/index.html 
14 alias liwag bbs site
15 [root@e46ae471064e bbs]# 
16 [root@e46ae471064e bbs]# curl localhost/bbs/
17 alias liwag bbs site
18 [root@e46ae471064e bbs]# 

5、nginx rewrite:

语法:
rewrite ^/(.*) url/$1 permanent;
意思是只要匹配如下^/(.*),就跳转至url上,且$1是取前面regex部分括号的内容,结尾的primanent是永久301重定向标记。

regex语法:
\:去掉特殊字符
^:起始位置
$:结束位置
*:匹配0次或多次
+:匹配前面的字符一次或多次
?:匹配前面的字符0次或1次
.:匹配\n之外的任何字符
(pattern):匹配括号内字符,并可以在后面匹配,常用$0-$9属性获取值

rewrite flag标记
last:本条规则匹配完成后,继续向下匹配
break:本条规则匹配完成后,不再匹配后面的规则
redirect:返回302临时重定向,
permanent:返回301永久重定向

例子,为实现需求,访问 网址/blog 时跳转至www.cnblogs.com:

 1 [root@e46ae471064e conf]# sed -n "33,39p" nginx.conf
 2     #gzip  on;
 3 
 4     server {
 5         listen       80;
 6         server_name  localhost;
 7 
 8     rewrite ^/(.*)/blog http://www.cnblogs.com/$1 permanent;
 9 [root@e46ae471064e conf]#  curl -I localhost/wang-li/blog
10 HTTP/1.1 301 Moved Permanently
11 Server: HaiYan/0.0.1
12 Date: Fri, 11 May 2018 14:44:12 GMT
13 Content-Type: text/html
14 Content-Length: 185
15 Connection: keep-alive
16 Location: http://www.cnblogs.com/wang-li
17 
18 [root@e46ae471064e conf]# 

3.nginx调优

1.修改默认用户,有两种方式其一是修改nginx.conf中的内容,但是事先用户必须在系统中存在,其二为编译时就指定用户和用户组

其一:

1 [root@e46ae471064e conf]# grep "nginx" /etc/passwd
2 nginx:x:1000:1000::/home/nginx:/sbin/nologin
3 [root@e46ae471064e conf]# sed -n "3,6p" nginx.conf
4 user  nginx nginx;
5 worker_processes  1;
6 
7 #error_log  logs/error.log;
8 [root@e46ae471064e conf]# 

其二:

1 [root@e46ae471064e conf]# /usr/local/nginx/sbin/nginx -V
2 nginx version: HaiYan/0.0.1
3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
4 built with OpenSSL 1.0.2k-fips  26 Jan 2017
5 TLS SNI support enabled
6 configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx
7 [root@e46ae471064e conf]# 

在编译时指定user和group

2.设置nginx worker cpu核数和cpu affinity

设置nginx worker cpu核数一般设置为系统CPU核数或者是其倍数

查看系统cpu核数

1 [root@e46ae471064e conf]# cat /proc/cpuinfo | grep processor | wc -l
2 2
3 [root@e46ae471064e conf]# 

修改后nginx worker为:

1 [root@e46ae471064e conf]# sed -n "3,6p" nginx.conf
2 worker_processes  4;
3 
4 #error_log  logs/error.log;
5 #error_log  logs/error.log  notice;
6 [root@e46ae471064e conf]# 

cpu affinity([əˈfɪnɪti])的目的是为了设置cpu分布,让其均匀分布在各个cpu上

配置如下:

1 [root@e46ae471064e conf]# sed -n "3,5p" nginx.conf
2 worker_processes  4;
3 worker_cpu_affinity 00000001 00000010 00000011 00000100;
4 
5 [root@e46ae471064e conf]# 

配置之后top命令如下:

 1 [root@e46ae471064e conf]# top -n 1
 2 
 3 top - 16:35:56 up  3:12,  0 users,  load average: 0.00, 0.00, 0.00
 4 Tasks:   7 total,   1 running,   6 sleeping,   0 stopped,   0 zombie
 5 %Cpu(s):  3.2 us,  0.0 sy,  0.0 ni, 96.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 6 KiB Mem :  2046976 total,   398764 free,   224528 used,  1423684 buff/cache
 7 KiB Swap:  1048572 total,  1048572 free,        0 used.  1646940 avail Mem 
 8 
 9   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                   
10     1 root      20   0   11784   2936   2492 S   0.0  0.1   0:00.56 bash                                                                                                                      
11   185 root      20   0   45932   3612   2588 S   0.0  0.2   0:00.01 nginx                                                                                                                     
12   279 nginx     20   0   73068  30652   2492 S   0.0  1.5   0:00.00 nginx                                                                                                                     
13   280 nginx     20   0   73068  30652   2492 S   0.0  1.5   0:00.00 nginx                                                                                                                     
14   281 nginx     20   0   73068  30652   2492 S   0.0  1.5   0:00.00 nginx                                                                                                                     
15   282 nginx     20   0   73068  30616   2456 S   0.0  1.5   0:00.00 nginx                                                                                                                     
16   283 root      20   0   51908   3676   3152 R   0.0  0.2   0:00.00 top   

3.调整nginx 工作模式为epoll,来处理高并发事件

1 [root@e46ae471064e conf]# sed -n "12,17p" nginx.conf
2 
3 events {
4     use epoll;
5     worker_connections  1024;
6 }
7 
8 [root@e46ae471064e conf]# 

4.设置nginx worker进程最大打开文件描述符数目,最好还是与系统ulimit保持一致

1 [root@e46ae471064e conf]# ulimit -n
2 1048576
3 [root@e46ae471064e conf]# 

配置conf如下:

1 [root@e46ae471064e conf]# sed -n "3,6p" nginx.conf
2 worker_processes  4;
3 worker_cpu_affinity 00000001 00000010 00000011 00000100;
4 worker_rlimit_nofile 1048576;
5 
6 [root@e46ae471064e conf]# 

5.配置nginx最多的连接数

配置如下:

 1 [root@e46ae471064e conf]# sed -n "10,20p" nginx.conf
 2 
 3 #pid        logs/nginx.pid;
 4 
 5 
 6 events {
 7     use epoll;
 8     worker_connections  65535;
 9 }
10 
11 
12 http {
13 [root@e46ae471064e conf]# 

6.优化超时连接时间

当超过设定时间还未连接成功的话,则会断开

1 [root@e46ae471064e conf]# sed -n "30,35p" nginx.conf
2     sendfile        on;
3     #tcp_nopush     on;
4 
5     #keepalive_timeout  0;
6     keepalive_timeout  60;
7 
8 [root@e46ae471064e conf]# 

7.优化客户端请求头超时时间以及请求body超时时间

此设置主要是为了防止客户端利用http进行攻击,如果在设置时间内没有发送完整的header或则body,则会返回request time out错误

1 [root@e46ae471064e conf]# sed -n "34,39p" nginx.conf
2     keepalive_timeout  60;
3 
4     client_header_timeout 30;
5     client_body_timeout 60;
6 
7     #gzip  on;
8 [root@e46ae471064e conf]# 

8.优化客户端超时时间
如果超过设置时间,客户端还未有任何动作,nginx则会断掉此连接

1 [root@e46ae471064e conf]# sed -n "34,39p" nginx.conf
2     keepalive_timeout  60;
3 
4     client_header_timeout 30;
5     client_body_timeout 60;
6     send_timeout 30;
7 
8 [root@e46ae471064e conf]# 

附上nginx.conf如下(仅供参考)

 1 [root@e46ae471064e conf]# cat nginx.conf | grep -v "^$" | grep -v "#"
 2 worker_processes  4;
 3 worker_cpu_affinity 00000001 00000010 00000011 00000100;
 4 worker_rlimit_nofile 1048576;
 5 events {
 6     use epoll;
 7     worker_connections  65535;
 8 }
 9 http {
10     include       mime.types;
11     default_type  application/octet-stream;
12     sendfile        on;
13     keepalive_timeout  60;
14     client_header_timeout 30;
15     client_body_timeout 60;
16     send_timeout 30;
17     server {
18         listen       80;
19         server_name  localhost;
20     rewrite ^/(.*)/blog http://www.cnblogs.com/$1 permanent;
21     
22     
23     location ^~ /bbs/ {
24         alias   html/bbs/;
25         index index.html;
26     }
27         location / {
28             root   html;
29             index  index.html index.htm;
30     }
31 }}
32 [root@e46ae471064e conf]# 

4.性能测试

利用ab压力测试工具进行测试结果如下:

 1 [root@e46ae471064e conf]# ab -c 10 -n 10000 localhost/
 2 参数说明,-c 为并发数 -n 为总请求数 总请求数需要大于并发数
 3 This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
 4 Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
 5 Licensed to The Apache Software Foundation, http://www.apache.org/
 6 上述介绍ab工具版本等等信息
 7 
 8 Benchmarking localhost (be patient)
 9 Completed 1000 requests
10 Completed 2000 requests
11 Completed 3000 requests
12 Completed 4000 requests
13 Completed 5000 requests
14 Completed 6000 requests
15 Completed 7000 requests
16 Completed 8000 requests
17 Completed 9000 requests
18 Completed 10000 requests
19 Finished 10000 requests
20 
21 
22 Server Software:        HaiYan/0.0.1
23 Server Hostname:        localhost
24 Server Port:            80
25 show 出服务器信息以及访问信息端口等
26 
27 Document Path:          /
28 Document Length:        52273 bytes
29 请求路劲以及文件大小
30 
31 Concurrency Level:      10
32 Time taken for tests:   1.024 seconds
33 整个测试持续的时间
34 Complete requests:      10000
35 完成的请求数量
36 Failed requests:        0
37 失败数
38 Write errors:           0
39 Total transferred:      525090000 bytes
40 HTML transferred:       522730000 bytes
41 Requests per second:    9769.13 [#/sec] (mean)
42 平均返回数据时间
43 Time per request:       1.024 [ms] (mean)
44 平均响应时间
45 Time per request:       0.102 [ms] (mean, across all concurrent requests)
46 平均并发响应时间
47 Transfer rate:          500944.38 [Kbytes/sec] received
48 
49 Connection Times (ms)
50               min  mean[+/-sd] median   max
51 Connect:        0    0   0.1      0       2
52 Processing:     0    1   0.2      1       3
53 Waiting:        0    0   0.2      0       2
54 Total:          0    1   0.2      1       3
55 
56 响应时间的值
57 
58 Percentage of the requests served within a certain time (ms)
59   50%      1
60   66%      1
61   75%      1
62   80%      1
63   90%      1
64   95%      1
65   98%      2
66   99%      2
67  100%      3 (longest request)
68  请求的平均速度
69 [root@e46ae471064e conf]# 

2.测试未调优之前的nginx

 1 [root@e46ae471064e conf]# ab -c 2000 -n 30000 localhost/
 2 This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
 3 Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
 4 Licensed to The Apache Software Foundation, http://www.apache.org/
 5 
 6 Benchmarking localhost (be patient)
 7 Completed 3000 requests
 8 Completed 6000 requests
 9 Completed 9000 requests
10 Completed 12000 requests
11 Completed 15000 requests
12 Completed 18000 requests
13 Completed 21000 requests
14 Completed 24000 requests
15 Completed 27000 requests
16 Completed 30000 requests
17 Finished 30000 requests
18 
19 
20 Server Software:        HaiYan/0.0.1
21 Server Hostname:        localhost
22 Server Port:            80
23 
24 Document Path:          /
25 Document Length:        0 bytes
26 
27 Concurrency Level:      2000
28 Time taken for tests:   3.910 seconds
29 Complete requests:      30000
30 Failed requests:        29911
31    (Connect: 0, Receive: 0, Length: 28919, Exceptions: 992)
32 Write errors:           0
33 Total transferred:      1518507771 bytes
34 HTML transferred:       1511682887 bytes
35 Requests per second:    7672.50 [#/sec] (mean)
36 Time per request:       260.671 [ms] (mean)
37 Time per request:       0.130 [ms] (mean, across all concurrent requests)
38 Transfer rate:          379256.45 [Kbytes/sec] received
39 
40 Connection Times (ms)
41               min  mean[+/-sd] median   max
42 Connect:        0   75 251.3      7    1118
43 Processing:     5   61 202.7     34    1897
44 Waiting:        0   37 190.3      9    1897
45 Total:          7  135 393.5     47    3006
46 
47 Percentage of the requests served within a certain time (ms)
48   50%     47
49   66%     53
50   75%     55
51   80%     57
52   90%    157
53   95%   1054
54   98%   1531
55   99%   2027
56  100%   3006 (longest request)
57 [root@e46ae471064e conf]# 

3.测试调优之后的nginx

 1 [root@e46ae471064e conf]# ab -c 2000 -n 30000 localhost/
 2 This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
 3 Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
 4 Licensed to The Apache Software Foundation, http://www.apache.org/
 5 
 6 Benchmarking localhost (be patient)
 7 Completed 3000 requests
 8 Completed 6000 requests
 9 Completed 9000 requests
10 Completed 12000 requests
11 Completed 15000 requests
12 Completed 18000 requests
13 Completed 21000 requests
14 Completed 24000 requests
15 Completed 27000 requests
16 Completed 30000 requests
17 Finished 30000 requests
18 
19 
20 Server Software:        HaiYan/0.0.1
21 Server Hostname:        localhost
22 Server Port:            80
23 
24 Document Path:          /
25 Document Length:        52273 bytes
26 
27 Concurrency Level:      2000
28 Time taken for tests:   5.894 seconds
29 Complete requests:      30000
30 Failed requests:        0
31 Write errors:           0
32 Total transferred:      1575270000 bytes
33 HTML transferred:       1568190000 bytes
34 Requests per second:    5090.28 [#/sec] (mean)
35 Time per request:       392.906 [ms] (mean)
36 Time per request:       0.196 [ms] (mean, across all concurrent requests)
37 Transfer rate:          261020.94 [Kbytes/sec] received
38 
39 Connection Times (ms)
40               min  mean[+/-sd] median   max
41 Connect:        0   82  29.7     78     183
42 Processing:    48  304  70.2    316     476
43 Waiting:        0   63  29.5     59     176
44 Total:        110  385  61.2    391     561
45 
46 Percentage of the requests served within a certain time (ms)
47   50%    391
48   66%    409
49   75%    420
50   80%    426
51   90%    450
52   95%    471
53   98%    474
54   99%    491
55  100%    561 (longest request)
56 [root@e46ae471064e conf]# 

通过上述比对,即可发现调优之前和调优之后的差距,可以清晰的看到未调优前测试失败的请求数量29911,而调优后失败的请求数量为0。

猜你喜欢

转载自www.cnblogs.com/wang-li/p/9030718.html