原创

nginx proxy_cache 缓存配置

示例代码

http {

    # 缓存配置
    proxy_cache_path test_cache levels=1:2 keys_zone=test_cache:1024m max_size=200g inactive=365d use_temp_path=off;

    server {

        # 需要缓存的请求
        location /test/ {
            proxy_cache test_cache;
            proxy_cache_valid 200 365d;
            proxy_cache_key $uri;
            proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
            proxy_pass http://127.0.0.1:8080/test/;
        }

    }

}

说明

proxy_cache_path 缓存文件路径
levels 设置缓存文件目录层次;levels=1:2 表示两级目录
keys_zone 设置缓存名字和共享内存大小
max_size 最大缓存空间,如果缓存空间满,默认覆盖掉缓存时间最长的资源。
inactive 在指定时间内没人访问则被删除

proxy_cache test_cache; 使用名为 test_cache 的对应缓存配置
proxy_cache_valid 200 365d; 对httpcode为200的缓存365天
proxy_cache_key $uri 定义缓存唯一key,通过唯一key来进行hash存取
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 忽略请求头,否则缓存可能不生效

正文到此结束