将静态资源如css/js/图片缓存到不用每次都到后端服务去拿取,减少性能消耗。
在nginx配置文件,httpd内部配置下面内容
proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=blog-assets-cache:30m inactive=1d max_size=1g;
不改变之前的location配置,在之前的location前面插入下面配置
location ~.*\.(png|jpg|css|js)$ {
expires 1d;
proxy_pass http://blogXqlee:8080;
proxy_cache blog-assets-cache;
proxy_cache_valid 200 1d;
proxy_cache_valid any 5m;
proxy_cache_key "$host$request_uri";
proxy_cache_revalidate on;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
add_header X-Proxy-Cache $upstream_cache_status;
}
nginx -s reload
或者 重启nginx
访问web应用,查看缓存目录是否有缓存文件
可以看到在缓存目录下已经生成很多目录(hash法)
http://blog.xqlee.com/article/2504180954226439.html