Nginx的缓存模块proxy_cache

Nginx缓存模块proxy_cache

    • 缓存配置语法
      • 缓存过期周期
      • 定义缓存的键,缓存的维度
    • Nginx缓存配置示例
      • 准备web节点
      • 准备Proxy节点
      • 访问测试
      • 清理Nginx缓存方式
    • 配置某些页面不缓存,并配置缓存日志
      • 部分缓存测试

缓存配置语法

Syntax: 	proxy_cache zone | off;
Default:	proxy_cache off;
Context:	http, server, location


## 设置缓存的路径和其他参数。缓存数据存储在文件中。缓存中的文件名是将MD5功能应用于 缓存键的结果。该levels参数定义高速缓存的层次结构级别:从1到3,每个级别接受值1或2.例如,在以下配置中
Syntax:	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];
Default:	—
Context:	http

# 示例
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
# 缓存中的文件名如下所示:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c

缓存过期周期

Syntax:         proxy_cache_valid [code ...] time;
Default:	—
Context:	http, server, location

## 示例
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;

定义缓存的键,缓存的维度

Syntax:	proxy_cache_key string;
Default:	
proxy_cache_key $scheme$proxy_host$request_uri;
Context:	http, server, location

## 示例
proxy_cache_key "$host$request_uri $cookie_user";
proxy_cache_key "$scheme$proxy_host$uri$is_args$args";

Nginx缓存配置示例

ip 服务器 功能
192.168.1.16 Nginx Proxy 调度请求
192.168.1.17 Nginx WEB 处理请求

准备web节点

[root@nginx /soft]# mkdir -p /soft/code{1..3}
[root@nginx /soft]# for i in {1..3};do echo "

Code1-URL$i

" > /soft/code1/url$i.html;done [root@nginx /soft]# for i in {1..3};do echo "

Code2-URL$i

" > /soft/code2/url$i.html;done [root@nginx /soft]# for i in {1..3};do echo "

Code3-URL$i

" > /soft/code3/url$i.html;done [root@nginx /etc/nginx/conf.d]# vim cache.conf server { listen 8081; root /soft/code1; location / { index index.html; } } server { listen 8082; root /soft/code2; location / { index index.html; } } server { listen 8083; root /soft/code3; location / { index index.html; } } ## 访问测试 [root@nginx /etc/nginx/conf.d]# nginx -s reload [root@proxy /usr/share/nginx/html]# curl http://192.168.1.17:8081/url1.html

Code1-URL1

[root@proxy /usr/share/nginx/html]# curl http://192.168.1.17:8081/url2.html

Code1-URL2

[root@proxy /usr/share/nginx/html]# curl http://192.168.1.17:8081/url3.html

Code1-URL3

[root@proxy /usr/share/nginx/html]# curl http://192.168.1.17:8082/url3.html

Code2-URL3

[root@proxy /usr/share/nginx/html]# curl http://192.168.1.17:8082/url1.html

Code2-URL1

[root@proxy /usr/share/nginx/html]# curl http://192.168.1.17:8083/url1.html

Code2-URL1

[root@proxy /usr/share/nginx/html]#

准备Proxy节点

[root@proxy /etc/nginx/conf.d]# vim proxy.conf
# level=1:2缓存的层次结构为2层
# keys_zone缓存空间名称为code_cache:10m,大小为10兆
# max_size=10g最大文件缓存为10G,超过10GNginx的裁判机制会剔除不经常被访问的缓存
# inactive=60m该缓存60分钟内没被访问就把清理掉
# use_temp_path=off会生成一个临时的.tmp缓存,会和自己定义的/soft/cache缓存冲突,导致性能下降,先关闭掉

proxy_cache_path /soft/cache level=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;

upstream cache {
        server 192.168.1.17:8081;
        server 192.168.1.17:8082;
        server 192.168.1.17:8083;
}

server {
        listen 80;
        server_name 192.168.1.16;
        include /etc/nginx/conf.d/proxy_params;
        location / {
                proxy_pass http://cache;
                proxy_cache code_cache;
                # 状态码为200 304的缓存12小时
                proxy_cache_valid 200 304 12h;
                # 其余的缓存10分钟
                proxy_cache_valid any 10m;
                # response响应的头信息中定义缓存的状态(有没有命中)
                add_header Nginx-Cache "$upstream_cache_status";
                # 如果出现以下error,或者50*的状态码,将会重新负载调度到另一台服务器上重新请求(不会在一台服务器上死磕)
                proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        }
}


访问测试

  • 第一次访问生成缓存
[root@proxy /soft/cache]# tree
.
└── 8
    └── c5
        └── b39aea32bccf0c3e468f726ae9c75c58

2 directories, 1 file
[root@proxy /soft/cache]# 
[root@proxy /etc/nginx/conf.d]# curl -I http://192.168.1.16/url1.html
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Wed, 28 Nov 2018 11:47:05 GMT
Content-Type: text/html
Content-Length: 20
Connection: keep-alive
Last-Modified: Wed, 28 Nov 2018 10:49:18 GMT
ETag: "5bfe72ae-14"
Nginx-Cache: MISS       ### 没有命中
Accept-Ranges: bytes

[root@proxy /etc/nginx/conf.d]# 

Nginx的缓存模块proxy_cache_第1张图片

  • 第二次访问,缓存命中了
[root@proxy /etc/nginx/conf.d]# curl -I http://192.168.1.16/url1.html
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Wed, 28 Nov 2018 11:48:00 GMT
Content-Type: text/html
Content-Length: 20
Connection: keep-alive
Last-Modified: Wed, 28 Nov 2018 10:49:18 GMT
ETag: "5bfe72ae-14"
Nginx-Cache: HIT        ## 缓存命中了
Accept-Ranges: bytes

[root@proxy /etc/nginx/conf.d]# 

Nginx的缓存模块proxy_cache_第2张图片

  • 关闭Nginx缓存再测试
location / {
            proxy_pass http://cache;
            # 关闭Nginx缓存
            proxy_cache off;
            # 状态码为200 304的缓存12小时
            proxy_cache_valid 200 304 12h;
            # 其余的缓存10分钟
            proxy_cache_valid any 10m;
            # response响应的头信息中定义缓存的状态(有没有命中)
            add_header Nginx-Cache "$upstream_cache_status";
            # 如果出现以下error,或者50*的状态码,将会重新负载调度到另一台服务器上重新请求(不会在一台服务器上死磕)
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    }

Nginx的缓存模块proxy_cache_第3张图片

清理Nginx缓存方式

1. 方法一:删除缓存目录下的文件
[root@proxy /soft/cache]# ls
7  8
[root@proxy /soft/cache]# rm -rf *
[root@proxy /soft/cache]# ls
[root@proxy /soft/cache]# 

2. 方法二:使用第三方扩展模块:ngx_cache_purge
写在下一章

配置某些页面不缓存,并配置缓存日志

语法:定义不将响应保存到缓存的条件。

Syntax:	    proxy_no_cache string ...;
Default:	—
Context:	http, server, location
# 如果字符串参数的至少一个值不为空且不等于“0”
# (set $cookie_nocache 1; 非0为不缓存),则不会缓存响应
proxy_no_cache $ cookie_nocache $ arg_nocache $ arg_comment;
proxy_no_cache $ http_pragma $ http_authorization;

# 为了观察缓存的命中状态,我们可以将缓存相关的变量记录在日志中。
# nginx.conf中配置(在行尾加上'"$upstream_cache_status"'缓存命中状态即可)
[root@proxy /etc/nginx]# vim nginx.conf 
...
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"' '"$upstream_cache_status"'; 



# 配置不缓存的页面
[root@proxy /etc/nginx/conf.d]# vim proxy.conf +13
proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
upstream cache {
        server 192.168.1.17:8081;
        server 192.168.1.17:8082;
        server 192.168.1.17:8083;
}
server {
        listen 80;
        server_name 192.168.1.16;
        access_log  /var/log/nginx/proxy_cache.log  main; 
        include /etc/nginx/conf.d/proxy_params;
        if ( $request_uri ~* ^/(url3|login|register|password)) {
                set $cookie_nocache 1; #非0为不缓存
        }

        location / {
                proxy_pass http://cache;
                proxy_cache code_cache;
                # 状态码为200 304的缓存12小时
                proxy_cache_valid 200 304 12h;
                # 其余的缓存10分钟
                proxy_cache_valid any 10m;
                # response响应的头信息中定义缓存的状态(有没有命中)
                proxy_cache_key "$host$uri$is_args$args";
                expires 1d; #超期时间
                proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
                proxy_no_cache $http_pragma    $http_authorization;
                add_header Nginx-Cache "$upstream_cache_status";
                # 如果出现以下error,或者50*的状态码,将会重新负载调度到另一台服务器上重新请求(不会在一台服务器上死磕)
                proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        }
}

部分缓存测试

# 观察缓存访问日志可以看出url3.html没有被缓存,其余都正常缓存了
192.168.1.5 - - [29/Nov/2018:02:16:59 -0500] "HEAD /url1.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""MISS"
192.168.1.5 - - [29/Nov/2018:02:17:02 -0500] "HEAD /url1.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""HIT"
192.168.1.5 - - [29/Nov/2018:02:17:15 -0500] "HEAD /url1.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""HIT"
192.168.1.5 - - [29/Nov/2018:02:17:18 -0500] "HEAD /url3.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""MISS"
192.168.1.5 - - [29/Nov/2018:02:17:19 -0500] "HEAD /url3.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""MISS"
192.168.1.5 - - [29/Nov/2018:02:17:20 -0500] "HEAD /url3.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""MISS"
192.168.1.5 - - [29/Nov/2018:02:17:21 -0500] "HEAD /url3.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""MISS"
192.168.1.5 - - [29/Nov/2018:02:17:22 -0500] "HEAD /url3.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""MISS"
192.168.1.5 - - [29/Nov/2018:02:17:25 -0500] "HEAD /url2.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""MISS"
192.168.1.5 - - [29/Nov/2018:02:17:27 -0500] "HEAD /url2.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""HIT"
192.168.1.5 - - [29/Nov/2018:02:17:28 -0500] "HEAD /url2.html HTTP/1.1" 200 0 "-" "curl/7.29.0" "-""HIT"

你可能感兴趣的:(Nginx)