Nginx系列(三)——内容缓存

◇◆丶佛笑我妖孽 提交于 2020-08-11 23:51:57

Caching Zones
定义缓存空间
proxy_cache_path /var/nginx/cache    #缓存空间路径
keys_zone=CACHE:60m
levels=1:2
inactive=3h    #缓存时长,3小时内没有被再次请求就会被释放
max_size=20g;
proxy_cache CACHE;

Caching Hash Keys
使用proxy_cache_key模块
proxy_cache_key "$host$request_uri $cookie_user";

Cache Bypass
避开缓存。适用于进行troubleshooting和debug的时候
proxy_cache_bypass $http_cache_bypass;
另外还可以通过设置 proxy_cache off; 进行关闭缓存

Cache Performance
在用户端控制缓存
location ~* \.(css|js)$ {
    expires 1y;    #1年后过期
    add_header Cache-Control "public";        #配置http响应头缓存控制
}

Cache Slicing
缓存切片。适用于不会变动的大文件
proxy_cache_path /tmp/mycache keys_zone=mycache:10m;
server {
    ...
    proxy_cache mycache;
    slice 1m;    #以1m为单位进行切片
    proxy_cache_key $host$uri$is_args$args$slice_range;    #切片命名方式?
    proxy_set_header Range $slice_range;
    proxy_http_version 1.1;
    proxy_cache_valid 200 206 1h;
    location / {
    proxy_pass http://origin:80;
    }
}











































易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!