Nginx反向代理功能-缓存功能
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.未使用缓存时对nginx服务器做压力测试并记录结果
1>.自行配置nginx的反向代理
博主推荐阅读: https://www.cnblogs.com/yinzhengjie/p/12099808.html
试验架构说明:
node101.yinzhengjie.org.cn:
Nginx反向代理服务器
node108.yinzhengjie.org.cn:
Apache httpd web服务器
node105.yinzhengjie.org.cn:
ab工具压力测试服务器
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf worker_processes 4; worker_cpu_affinity 00000001 00000010 00000100 00001000; events { worker_connections 100000; use epoll; accept_mutex on; multi_accept on; } http { include mime.types; default_type text/html; server_tokens off; charset utf-8; log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_ti me,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}'; access_log logs/access_json.log my_access_json; ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt; ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; include /yinzhengjie/softwares/nginx/conf.d/*.conf; } [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf server { listen 80; listen 443 ssl; server_name node101.yinzhengjie.org.cn; access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json; error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log; location / { root /yinzhengjie/data/web/nginx/static/cn; index index.html; #定义有效的请求referer,用空格隔开即可 valid_referers none blocked server_names *.baidu.com example.* ~.google.; #如果没有在上面的有效链接定义那么均属于无效请求referer if ($invalid_referer) { return 403; } #如果是一些常见的压测试工具,咱们直接进给他拒绝访问 if ($http_user_agent ~ "ApacheBench|WebBench|TurnitinBot|Sougou web spider|Grid Server"){ return 403; } } location = /favicon.ico { root /yinzhengjie/data/web/nginx/images/jd; } location /app01 { #proxy_pass指令用来设置将客户端请求转发给的后端服务器的主机,可以是主机名、IP地址:端口的方式,也可以代理到预先设置的主机群组,需要模块gx_http_upstream_module支持。 #带斜线,等于访问后端服务器的http://172.30.108:80/index.html内容返回给客户端。 proxy_pass http://172.30.1.108/; #proxy_connect_timeout time;配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60秒,用法如下: proxy_connect_timeout 60s; } location /static { #不带斜线将访问的/static,等于访问后端服务器 http://172.30.1.108:80/static/index.html,即后端服务器配置的站点根目录要有/static目录才可以被访问。 proxy_pass http://172.30.1.108; } location /image { proxy_pass http://172.30.1.108; #proxy_hide_header指令用于nginx作为反向代理的时候,在返回给客户端http响应的时候,用于隐藏后端服务器特定的响应首部, #默认nginx在响应报文中不传递后端服务器的首部字段Dte, Server, XPad,X-Accel等,可以设置在http,server,location块. #隐藏掉ETag的文本值,CDN会根据ETag的值是否发生变化而决定该文件内容是否发生变化,一旦发生变化CDN会重新抓取该数据并缓存,此处我故意隐藏该值。 proxy_hide_header ETag; } location /dynamic { proxy_pass http://172.30.1.108; #proxy_set_header可以更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的时候,就要更改每一个报文的头部。 #添加HOST到报文头部,如果客户端为NAT上网那么其值为客户端的共用的公网IP地址。 proxy_set_header yinzhengjie_nginx_ip_forwarded $proxy_add_x_forwarded_for; } } [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node108.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf ServerRoot "/etc/httpd" Listen 80 Include conf.modules.d/*.conf User apache Group apache ServerAdmin root@localhost <Directory /> AllowOverride none Require all denied </Directory> DocumentRoot "/var/www/html" <Directory "/var/www"> AllowOverride None Require all granted </Directory> <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> <IfModule dir_module> DirectoryIndex index.html </IfModule> <Files ".ht*"> Require all denied </Files> ErrorLog "logs/error_log" LogLevel warn <IfModule log_config_module> LogFormat ""%{yinzhengjie_nginx_ip_forwarded}i" %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined LogFormat "%h %l %u %t "%r" %>s %b" common <IfModule logio_module> LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio </IfModule> CustomLog "logs/access_log" combined </IfModule> <IfModule alias_module> ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" </IfModule> <Directory "/var/www/cgi-bin"> AllowOverride None Options None Require all granted </Directory> <IfModule mime_module> TypesConfig /etc/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType text/html .shtml AddOutputFilter INCLUDES .shtml </IfModule> AddDefaultCharset UTF-8 <IfModule mime_magic_module> MIMEMagicFile conf/magic </IfModule> EnableSendfile on IncludeOptional conf.d/*.conf [root@node108.yinzhengjie.org.cn ~]# [root@node108.yinzhengjie.org.cn ~]#
2>.准备测试数据
[root@node108.yinzhengjie.org.cn ~]# ll -hR /var/www/html/ /var/www/html/: total 4.0K drwxr-xr-x 2 root root 24 Dec 26 14:19 dynamic drwxr-xr-x 2 root root 24 Dec 26 14:18 image -rw-r--r-- 1 root root 37 Dec 26 14:04 index.html drwxr-xr-x 2 root root 42 Dec 26 17:00 static /var/www/html/dynamic: total 4.0K -rw-r--r-- 1 root root 40 Dec 26 14:19 index.html /var/www/html/image: total 4.0K -rw-r--r-- 1 root root 38 Dec 26 14:18 index.html /var/www/html/static: total 348K -rw-r--r-- 1 root root 341K Dec 26 17:00 access.log -rw-r--r-- 1 root root 39 Dec 26 14:18 index.html [root@node108.yinzhengjie.org.cn ~]# [root@node108.yinzhengjie.org.cn ~]#
3>.在node105.yinzhengjie.org.cn使用ab命令对nginx进行压力测试
[root@node105.yinzhengjie.org.cn ~]# yum -y install httpd-tools Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.bit.edu.cn * extras: mirrors.huaweicloud.com * updates: mirror.bit.edu.cn base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 updates/7/x86_64/primary_db | 5.9 MB 00:00:01 Resolving Dependencies --> Running transaction check ---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-tools-2.4.6-90.el7.centos.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-tools-2.4.6-90.el7.centos.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.4.8-5.el7 will be installed ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================ Package Arch Version Repository Size ============================================================================================================================================================================ Installing: httpd-tools x86_64 2.4.6-90.el7.centos base 91 k Installing for dependencies: apr x86_64 1.4.8-5.el7 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k Transaction Summary ============================================================================================================================================================================ Install 1 Package (+2 Dependent packages) Total download size: 286 k Installed size: 584 k Downloading packages: (1/3): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:00 (2/3): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm | 91 kB 00:00:00 (3/3): apr-1.4.8-5.el7.x86_64.rpm | 103 kB 00:00:05 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 48 kB/s | 286 kB 00:00:05 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.4.8-5.el7.x86_64 1/3 Installing : apr-util-1.5.2-6.el7.x86_64 2/3 Installing : httpd-tools-2.4.6-90.el7.centos.x86_64 3/3 Verifying : apr-1.4.8-5.el7.x86_64 1/3 Verifying : httpd-tools-2.4.6-90.el7.centos.x86_64 2/3 Verifying : apr-util-1.5.2-6.el7.x86_64 3/3 Installed: httpd-tools.x86_64 0:2.4.6-90.el7.centos Dependency Installed: apr.x86_64 0:1.4.8-5.el7 apr-util.x86_64 0:1.5.2-6.el7 Complete! [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 242.545 seconds Complete requests: 100000 Failed requests: 848 (Connect: 0, Receive: 0, Length: 848, Exceptions: 0) Write errors: 0 Non-2xx responses: 767 Total transferred: 34617054848 bytes HTML transferred: 34591725127 bytes Requests per second: 412.30 [#/sec] (mean) Time per request: 4850.895 [ms] (mean) Time per request: 2.425 [ms] (mean, across all concurrent requests) Transfer rate: 139379.31 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 357 828.0 82 15103 Processing: 84 3767 6267.4 2446 138604 Waiting: 1 2235 5548.2 750 116412 Total: 86 4124 6317.0 2762 138716 Percentage of the requests served within a certain time (ms) 50% 2762 66% 3426 75% 3967 80% 4433 90% 6242 95% 10528 98% 19385 99% 32721 100% 138716 (longest request) [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 231.811 seconds Complete requests: 100000 Failed requests: 401 (Connect: 0, Receive: 0, Length: 401, Exceptions: 0) Write errors: 0 Non-2xx responses: 394 Total transferred: 34766790357 bytes HTML transferred: 34741426365 bytes Requests per second: 431.39 [#/sec] (mean) Time per request: 4636.213 [ms] (mean) Time per request: 2.318 [ms] (mean, across all concurrent requests) Transfer rate: 146464.11 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 452 1107.2 97 15208 Processing: 93 3582 5036.2 2618 114911 Waiting: 1 1903 4579.4 857 73779 Total: 109 4033 5131.7 3010 114972 Percentage of the requests served within a certain time (ms) 50% 3010 66% 3525 75% 3996 80% 4442 90% 6044 95% 9172 98% 17817 99% 30033 100% 114972 (longest request) [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 225.578 seconds Complete requests: 100000 Failed requests: 634 (Connect: 0, Receive: 0, Length: 634, Exceptions: 0) Write errors: 0 Non-2xx responses: 631 Total transferred: 34684484292 bytes HTML transferred: 34659141229 bytes Requests per second: 443.31 [#/sec] (mean) Time per request: 4511.555 [ms] (mean) Time per request: 2.256 [ms] (mean, across all concurrent requests) Transfer rate: 150154.73 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 441 1380.2 82 31163 Processing: 58 3554 5717.6 2413 120031 Waiting: 1 2131 5636.6 893 93156 Total: 62 3995 5863.4 2708 120125 Percentage of the requests served within a certain time (ms) 50% 2708 66% 3280 75% 3809 80% 4309 90% 6109 95% 9598 98% 19167 99% 33881 100% 120125 (longest request) [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 230.977 seconds Complete requests: 100000 Failed requests: 832 (Connect: 0, Receive: 0, Length: 832, Exceptions: 0) Write errors: 0 Non-2xx responses: 829 Total transferred: 34615832624 bytes HTML transferred: 34590507457 bytes Requests per second: 432.94 [#/sec] (mean) Time per request: 4619.532 [ms] (mean) Time per request: 2.310 [ms] (mean, across all concurrent requests) Transfer rate: 146354.77 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 310 611.3 79 15198 Processing: 95 3782 6025.0 2451 84089 Waiting: 2 2246 5877.4 976 67208 Total: 103 4092 6052.2 2701 84949 Percentage of the requests served within a certain time (ms) 50% 2701 66% 3277 75% 3786 80% 4325 90% 6014 95% 9715 98% 19764 99% 35307 100% 84949 (longest request) [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 258.968 seconds Complete requests: 100000 Failed requests: 1658 (Connect: 0, Receive: 0, Length: 1658, Exceptions: 0) Write errors: 0 Non-2xx responses: 1624 Total transferred: 34329476704 bytes HTML transferred: 34304223362 bytes Requests per second: 386.15 [#/sec] (mean) Time per request: 5179.360 [ms] (mean) Time per request: 2.590 [ms] (mean, across all concurrent requests) Transfer rate: 129455.68 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 457 1155.9 79 15073 Processing: 4 3902 7964.1 2252 112143 Waiting: 1 2540 7596.7 651 89124 Total: 4 4359 8038.9 2568 112190 Percentage of the requests served within a certain time (ms) 50% 2568 66% 3137 75% 3691 80% 4207 90% 6468 95% 10455 98% 33939 99% 53567 100% 112190 (longest request) [root@node105.yinzhengjie.org.cn ~]#
未配置Nginx缓存时,对Nginx压力测试我们以每秒完成请求数(Requests per second)作为参照点: 第一次测试Requests per second值: 412.30 [#/sec] (mean) 第二次测试Requests per second值: 431.39 [#/sec] (mean) 第三次测试Requests per second值: 443.31 [#/sec] (mean) 第四次测试Requests per second值: 432.94 [#/sec] (mean) 第五次测试Requests per second值: 386.15 [#/sec] (mean) 去掉一个最高值(443.31)和一个最低值(386.15),算得平均数为:"425 [#/sec] (mean)" 温馨提示: 如下图所示,测试时可能本地的CPU使用率会飙高,属于正常现象。
二.nginx配置缓存后再一次做压力测试并记录结果
1>.在nginx的主配置文件中定义可用于proxy功能的缓存。
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf worker_processes 4; worker_cpu_affinity 00000001 00000010 00000100 00001000; events { worker_connections 100000; use epoll; accept_mutex on; multi_accept on; } http { include mime.types; default_type text/html; server_tokens off; charset utf-8; log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_ti me,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}'; access_log logs/access_json.log my_access_json; #配置Nginx反向代理的缓存 proxy_cache_path /yinzhengjie/data/web/nginx/proxycache levels=1:2:2 keys_zone=proxycache:512m inactive=10m max_size=1g; ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt; ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; include /yinzhengjie/softwares/nginx/conf.d/*.conf; } [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nginx -t nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful [root@node101.yinzhengjie.org.cn ~]#
2>.在子配置文件中调用缓存功能
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf server { listen 80; listen 443 ssl; server_name node101.yinzhengjie.org.cn; access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json; error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log; location / { root /yinzhengjie/data/web/nginx/static/cn; index index.html; #定义有效的请求referer,用空格隔开即可 valid_referers none blocked server_names *.baidu.com example.* ~.google.; #如果没有在上面的有效链接定义那么均属于无效请求referer if ($invalid_referer) { return 403; } #如果是一些常见的压测试工具,咱们直接进给他拒绝访问 if ($http_user_agent ~ "ApacheBench|WebBench|TurnitinBot|Sougou web spider|Grid Server"){ return 403; } } location = /favicon.ico { root /yinzhengjie/data/web/nginx/images/jd; } location /app01 { proxy_pass http://172.30.1.108/; proxy_connect_timeout 60s; } location /static { proxy_pass http://172.30.1.108; #指明调用的缓存区,这个名称在Nginx的主配置文件中有定义 proxy_cache proxycache; #缓存中用于"键"的内容 proxy_cache_key $request_uri; #定义对特定响应码的响应内容的缓存时长 proxy_cache_valid 200 302 301 10m; proxy_cache_valid any 5m; } location /image { proxy_pass http://172.30.1.108; proxy_hide_header ETag; proxy_set_header yinzhengjie_nginx_ip_forwarded $proxy_add_x_forwarded_for; } location /dynamic { proxy_pass http://172.30.1.108; proxy_set_header yinzhengjie_nginx_ip_forwarded $proxy_add_x_forwarded_for; } } [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nginx -t nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/ #很明显,缓存目录配置成功啦,该目录不需要咱们创建,而是在使用语法检测时自动创建的。 total 0 [root@node101.yinzhengjie.org.cn ~]#
3>.重新加载nginx的配置文件
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep root 21509 1 0 16:34 ? 00:00:00 nginx: master process nginx nginx 21759 21509 0 17:33 ? 00:00:00 nginx: worker process nginx 21760 21509 0 17:33 ? 00:00:00 nginx: worker process nginx 21761 21509 0 17:33 ? 00:00:00 nginx: worker process nginx 21762 21509 0 17:33 ? 00:00:00 nginx: worker process nginx 21763 21509 0 17:33 ? 00:00:00 nginx: cache manager process nginx 21764 21509 0 17:33 ? 00:00:00 nginx: cache loader process [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nginx -s reload [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep root 21509 1 0 16:34 ? 00:00:00 nginx: master process nginx nginx 21782 21509 0 17:37 ? 00:00:00 nginx: worker process nginx 21783 21509 0 17:37 ? 00:00:00 nginx: worker process nginx 21784 21509 0 17:37 ? 00:00:00 nginx: worker process nginx 21785 21509 0 17:37 ? 00:00:00 nginx: worker process nginx 21786 21509 0 17:37 ? 00:00:00 nginx: cache manager process [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
4>.在node105.yinzhengjie.org.cn使用ab命令对nginx进行压力测试
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 175.025 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 34905100000 bytes HTML transferred: 34879700000 bytes Requests per second: 571.35 [#/sec] (mean) Time per request: 3500.501 [ms] (mean) Time per request: 1.750 [ms] (mean, across all concurrent requests) Transfer rate: 194755.07 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 490 851.9 165 7269 Processing: 117 2997 698.7 3160 6984 Waiting: 1 205 228.3 123 2027 Total: 118 3487 1032.6 3428 10223 Percentage of the requests served within a certain time (ms) 50% 3428 66% 3588 75% 3743 80% 3968 90% 4427 95% 5179 98% 6285 99% 6770 100% 10223 (longest request) [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 173.924 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 34905100000 bytes HTML transferred: 34879700000 bytes Requests per second: 574.96 [#/sec] (mean) Time per request: 3478.473 [ms] (mean) Time per request: 1.739 [ms] (mean, across all concurrent requests) Transfer rate: 195988.37 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 314 451.2 148 3278 Processing: 259 3144 661.4 3197 8202 Waiting: 0 244 419.5 122 4904 Total: 466 3457 798.2 3407 8355 Percentage of the requests served within a certain time (ms) 50% 3407 66% 3493 75% 3637 80% 3845 90% 4305 95% 4623 98% 6017 99% 6370 100% 8355 (longest request) [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 172.614 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 34905100000 bytes HTML transferred: 34879700000 bytes Requests per second: 579.33 [#/sec] (mean) Time per request: 3452.287 [ms] (mean) Time per request: 1.726 [ms] (mean, across all concurrent requests) Transfer rate: 197474.96 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 275 338.1 153 3355 Processing: 2 3166 464.8 3246 8819 Waiting: 0 189 222.7 117 1900 Total: 2 3442 562.0 3436 9899 Percentage of the requests served within a certain time (ms) 50% 3436 66% 3495 75% 3556 80% 3636 90% 4157 95% 4449 98% 4866 99% 5166 100% 9899 (longest request) [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 172.927 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 34905100000 bytes HTML transferred: 34879700000 bytes Requests per second: 578.28 [#/sec] (mean) Time per request: 3458.539 [ms] (mean) Time per request: 1.729 [ms] (mean, across all concurrent requests) Transfer rate: 197117.96 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 1 318 440.8 155 3311 Processing: 285 3130 615.1 3215 7415 Waiting: 0 230 361.8 119 4259 Total: 424 3448 744.7 3436 7538 Percentage of the requests served within a certain time (ms) 50% 3436 66% 3491 75% 3561 80% 3739 90% 4243 95% 4692 98% 5574 99% 5928 100% 7538 (longest request) [root@node105.yinzhengjie.org.cn ~]#
[root@node105.yinzhengjie.org.cn ~]# ab -n100000 -c 2000 http://node101.yinzhengjie.org.cn/static/access.log This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking node101.yinzhengjie.org.cn (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: Tengine Server Hostname: node101.yinzhengjie.org.cn Server Port: 80 Document Path: /static/access.log Document Length: 348797 bytes Concurrency Level: 2000 Time taken for tests: 172.397 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 34905100000 bytes HTML transferred: 34879700000 bytes Requests per second: 580.06 [#/sec] (mean) Time per request: 3447.941 [ms] (mean) Time per request: 1.724 [ms] (mean, across all concurrent requests) Transfer rate: 197723.90 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 412 613.4 156 7132 Processing: 309 3020 694.8 3134 7696 Waiting: 0 241 409.1 118 4803 Total: 378 3432 894.5 3408 10841 Percentage of the requests served within a certain time (ms) 50% 3408 66% 3506 75% 3725 80% 3914 90% 4318 95% 4864 98% 5992 99% 6444 100% 10841 (longest request) [root@node105.yinzhengjie.org.cn ~]# [root@node105.yinzhengjie.org.cn ~]#
未配置Nginx缓存时,对Nginx压力测试我们以每秒完成请求数(Requests per second)作为参照点: 第一次测试Requests per second值: 571.35 [#/sec] (mean) 第二次测试Requests per second值: 574.96 [#/sec] (mean) 第三次测试Requests per second值: 579.33 [#/sec] (mean) 第四次测试Requests per second值: 578.28 [#/sec] (mean) 第五次测试Requests per second值: 580.06 [#/sec] (mean) 去掉一个最高值(580.06)和一个最低值(571.35),算得平均数为:"577 [#/sec] (mean)"。 温馨提示: 相比未加速前的"425 [#/sec] (mean)"来说,的确是优速提示,在原来的基础上提升了35%的速度(计算方式:"(577-425) * 100 / 425 ")
5>.查看缓存目录
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/ total 0 drwx------ 3 nginx nginx 16 Dec 26 17:37 4 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/4/ total 0 drwx------ 3 nginx nginx 16 Dec 26 17:37 61 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/4/61/ total 0 drwx------ 2 nginx nginx 46 Dec 26 17:37 8a [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/4/61/8a/ #我们发现被缓存的文件后缀5为个数字是该文件存储的目录名称,但是该缓存文件明显比真正的文件要大一点。这是为什么呢?你先猜猜看,接下来跟我一起来揭晓答案。 total 344 -rw------- 1 nginx nginx 349426 Dec 26 17:37 8163c0ca4d4d0c1ec72229042cf8a614 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# tail -1 /yinzhengjie/data/web/nginx/proxycache/4/61/8a/8163c0ca4d4d0c1ec72229042cf8a614 #出于好奇,我查看了缓存文件的内容,的确保存着数据的。 {"@timestamp":"2019-12-26T16:37:14+08:00","host":"172.30.1.101","clientip":"172.30.1.254","size":25214,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host ":"node101.yinzhengjie.org.cn","uri":"/favicon.ico","domain":"node101.yinzhengjie.org.cn","xff":"-","referer":"http://node101.yinzhengjie.org.cn/","tcp_xff":"","http_user_a gent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","status":"200"} [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ssh node108.yinzhengjie.org.cn #为了验证我的猜想,我远程到Apache httpd服务器上,查看该文件的最后一行数据进行对比。 Last login: Thu Dec 26 15:27:02 2019 from 172.30.1.254 [root@node108.yinzhengjie.org.cn ~]# [root@node108.yinzhengjie.org.cn ~]# ll /var/www/html/static/ total 348 -rw-r--r-- 1 root root 348797 Dec 26 17:00 access.log -rw-r--r-- 1 root root 39 Dec 26 14:18 index.html [root@node108.yinzhengjie.org.cn ~]# [root@node108.yinzhengjie.org.cn ~]# tail -1 /var/www/html/static/access.log #登录到服务器后我来查看该文件的最后一行,发现数据和Nginx缓存的完全吻合,但是我很好奇为什么Nginx缓存的文件会稍微打一点呢?我猜测是Apache httpd发送给Nginx数据时可能会夹杂着响应报文之类的信息,这才是导致Nginx缓存文件要稍大于Apche Httpd帧数保存的数据文件。 {"@timestamp":"2019-12-26T16:37:14+08:00","host":"172.30.1.101","clientip":"172.30.1.254","size":25214,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host ":"node101.yinzhengjie.org.cn","uri":"/favicon.ico","domain":"node101.yinzhengjie.org.cn","xff":"-","referer":"http://node101.yinzhengjie.org.cn/","tcp_xff":"","http_user_a gent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","status":"200"} [root@node108.yinzhengjie.org.cn ~]# [root@node108.yinzhengjie.org.cn ~]# exit #为了验证我的想法,立马返回Nginx服务器 logout Connection to node108.yinzhengjie.org.cn closed. [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# head -15 /yinzhengjie/data/web/nginx/proxycache/4/61/8a/8163c0ca4d4d0c1ec72229042cf8a614 #我们查看缓存文件的头部,果不其然,该文件的头部的确是有响应报文,这样用户访问时,可以直接将这个文件发送给请求的用户而无需再将数据封装成报文啦!从而速度得到提示。 ^ v^5^镥㧁u"5527d-59a979b183760" KEY: /static/access.log HTTP/1.1 200 OK Date: Thu, 26 Dec 2019 10:20:21 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Thu, 26 Dec 2019 09:00:16 GMT ETag: "5527d-59a979b183760" Accept-Ranges: bytes Content-Length: 348797 Connection: close Content-Type: text/plain; charset=UTF-8 {"@timestamp":"2019-12-26T16:35:12+08:00","host":"172.30.1.101","clientip":"172.30.1.254","size":566,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host": "node101.yinzhengjie.org.cn","uri":"/index.html","domain":"node101.yinzhengjie.org.cn","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 10.0;
Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","status":"200"}
{"@timestamp":"2019-12-26T16:35:12+08:00","host":"172.30.1.101","clientip":"172.30.1.254","size":1025154,"responsetime":0.004,"upstreamtime":"-","upstreamhost":"-","http_ho st":"node101.yinzhengjie.org.cn","uri":"/css/01.png","domain":"node101.yinzhengjie.org.cn","xff":"-","referer":"http://node101.yinzhengjie.org.cn/","tcp_xff":"","http_user_
agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","status":"200"}
{"@timestamp":"2019-12-26T16:35:24+08:00","host":"172.30.1.101","clientip":"172.30.1.254","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"n ode101.yinzhengjie.org.cn","uri":"/index.html","domain":"node101.yinzhengjie.org.cn","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; W
in64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","status":"304"}
[root@node101.yinzhengjie.org.cn ~]#
6>.等待10分钟后,再查看缓存目录
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/ total 0 drwx------ 3 nginx nginx 16 Dec 26 17:37 4 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/4/ total 0 drwx------ 3 nginx nginx 16 Dec 26 17:37 61 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/4/61/ total 0 drwx------ 2 nginx nginx 6 Dec 26 18:08 8a [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/proxycache/4/61/8a/ #10分钟后我发现之前的缓存文件自动被清理啦。这是因为我们在Nginx的子配置文件中已经定义好了文件的缓存时间。 total 0 [root@node101.yinzhengjie.org.cn ~]#
三.Nginx服务器缓存相关功能常用的配置指令说明
1>.proxy_cache
指明调用的缓存,或关闭缓存机制,默认关闭,可配置在http,server,location区域。 博主推荐阅读: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache
2>.proxy_cache_key
缓存中用于“键”的内容,默认值:"proxy_cache_key $scheme$proxy_host$request_uri;",可配置在http, server, location区域。 博主推荐阅读: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key
3>.proxy_cache_valid
定义对特定响应码的响应内容的缓存时长,可配置在:https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid 博主推荐阅读: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid
4>.proxy_cache_path
定义可用于proxy功能的缓存,默认是没有配置的,可配置在http中,语法格式如下: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; 示例: proxy_cache_path /yinzhengjie/data/web/nginx/proxycache levels=1:2:2 keys_zone=proxycache:512m inactive=10m max_size=1g; 以上参数说明: "/yinzhengjie/data/web/nginx/proxycache":
定义缓存保存路径,proxycache目录会自动创建。 "levels=1:2:2":
定义目录层级结构,1:2:2可以生成2^4*2^8*2^8=1048576个目录。 "keys_zone=proxycache:512m":
指定内存中缓存的大小,主要用于存放key和metadata(如缓存文件命中次数),建议不要设置的太小,也不要设置的过大。根据自己的服务器剩余空间来设置合理的值。 "inactive=10m" 指定缓存有效时间,若超出该时间的缓存文件会被删除哟~ "max_size=1g": 最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值。 博主推荐阅读: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_path
5>.proxy_cache_use_stale
在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端,默认是关闭的,可配置在http, server, location。语法格式如下: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...; 博主推荐阅读: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_use_stale
6>.proxy_cache_methods
对哪些客户端请求方法对应的响应进行缓存,默认GET和HEAD方法总是被缓存。可配置于 http, server, location。 博主推荐阅读: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_methods