1,在配置文件 server上方 增加缓存配置:
# 代理缓存配置
proxy_cache_path "./hyw_cachedata" levels=1:2 keys_zone=hywcache:256m inactive=1d max_size=1000g;
看下图
2,需要反向代理的地址增加缓存
location / {
proxy_pass http://127.0.0.1:8080/;
#root html;
#alias E:\\nginx-1.18.0/test/;
#proxy_pass http://backServer;
index index.html index.htm;
#使用缓存名称
proxy_cache hywcache;
#对以下状态码实现缓存
proxy_cache_valid 200 206 304 301 302 1d;
#缓存的key
proxy_cache_key $request_uri;
add_header X-Cache-Status $upstream_cache_status;
}
上图是把所有url都加入缓存了
测试:
a,启动应用
该有个5m的图片 和内容
b,把应用停止
一样正常访问,因为把页面的数据和图片都缓存到nginx目录下了