资讯专栏INFORMATION COLUMN

nginx proxy cache配置参数解读

alanoddsoff / 3341人阅读

本文主要解析一下nginx ngx_http_proxy_module中的cache相关配置参数。

proxy_cache
名称 默认配置 作用域 官方说明 中文解读 模块
proxy_cache proxy_cache off; http, server, location Defines a shared memory zone used for caching. The same zone can be used in several places. Parameter value can contain variables (1.7.9). The off parameter disables caching inherited from the previous configuration level. 设置是否开启对后端响应的缓存,如果开启的话,参数值就是zone的名称,比如proxy_cache mycache ngx_http_proxy_module
proxy_cache_valid 没有默认值,实例如proxy_cache_valid 200 302 10m; http, server, location Sets caching time for different response codes. 针对不同的response code设定不同的缓存时间,如果不设置code,默认为200,301,302,也可以用any指定所有code ngx_http_proxy_module
proxy_cache_key proxy_cache_key $scheme$proxy_host$request_uri; http, server, location Defines a key for caching 给缓存设定key,默认值相当于proxy_cache_key $scheme$proxy_host$uri$is_args$args; ngx_http_proxy_module
proxy_cache_path 没有默认值,实例proxy_cache_path /var/cache levels=1:2 keys_zone=imgcache:100m inactive=2h max_size=1g; http Sets the path and other parameters of a cache. Cache data are stored in files. The file name in a cache is a result of applying the MD5 function to the cache key. The levels parameter defines hierarchy levels of a cache: from 1 to 3, each level accepts values 1 or 2. 指定缓存存储的路径,文件名为cache key的md5值,然后多级目录的话,根据level参数来生成,比如levels=1:2:3,第一个目录名取md5值的倒数第一个值,第二个目录名取md5值的第2和3个值,第三个目录名取md5值的第4,5,6个值;key_zone参数用来指定在共享内存中缓存的元数据的名称和内存大小,比如keys_zone=imgcache:100m,所有的缓存查找首先经过这里查找元数据,如果命中再去文件系统查找相应的缓存 ;inactive用来指定缓存没有被访问超时移除的时间,默认是10分钟,也可以自己指定比如inactive=2h ;max_size 用来指定缓存的最大值,超过这个值则会自动移除最近最少使用的缓存 ngx_http_proxy_module
proxy_cache_bypass 没有默认值 http, server, location Defines conditions under which the response will not be taken from a cache. If at least one value of the string parameters is not empty and is not equal to “0” then the response will not be taken from the cache. 指定哪些响应在某些值不为空或不为0的情况下不走缓存,比如proxy_cache_bypass $http\_pragma $http_authorization; ngx_http_proxy_module
proxy_cache_min_uses proxy_cache_min_uses 1; http, server, location Sets the number of requests after which the response will be cached. 指定在多少次请求之后才缓存响应内容 ngx_http_proxy_module
proxy_cache_use_stale proxy_cache_use_stale off; http, server, location Determines in which cases a stale cached response can be used during communication with the proxied server. The directive’s parameters match the parameters of the proxy_next_upstream directive. 指定在后端服务器在返回什么状态码的情况下可以使用过期的缓存,比如proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504; ngx_http_proxy_module
proxy_cache_lock proxy_cache_lock off; http, server, location When enabled, only one request at a time will be allowed to populate a new cache element identified according to the proxy_cache_key directive by passing a request to a proxied server. Other requests of the same cache element will either wait for a response to appear in the cache or the cache lock for this element to be released, up to the time set by the proxy_cache_lock_timeout directive. 默认不开启,开启的话则每次只能有一个请求更新相同的缓存,其他请求要么等待缓存有数据要么限时等待锁释放;nginx 1.1.12才开始有 ngx_http_proxy_module
proxy_cache_lock_timeout proxy_cache_lock_timeout 5s; http, server, location Sets a timeout for proxy_cache_lock. When the time expires, the request will be passed to the proxied server, however, the response will not be cached. 等待缓存锁超时之后将直接请求后端,结果不会被缓存 ; nginx 1.1.12才开始有 ngx_http_proxy_module
实例
http {
    # we set this to be on the same filesystem as proxy_cache_path
    proxy_temp_path /usr/local/nginx/proxy_temp;
    # good security practice dictates that this directory is owned by the
    # same user as the user directive (under which the workers run)
    proxy_cache_path /usr/local/nginx/proxy_temp keys_zone=CACHE:10m levels=1:2 inactive=6h max_size=1g;

    server {
        location / {
            # using include to bring in a file with commonly-used settings
            include proxy.conf;
            # referencing the shared memory zone defined above
            proxy_cache CACHE;
            proxy_cache_valid any 1d;
            proxy_cache_bypass $http_pragma $http_authorization;
            proxy_cache_min_uses 3;
            proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
            proxy_pass http://upstream;
        }
    }
}
doc

ngx_http_proxy_module

nginx反向代理缓存配置

Understanding the nginx proxy_cache_path directive

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39755.html

相关文章

  • nginx gzip配置参数解读

    序 本文主要解析一下nginx ngx_http_gzip_module以及ngx_http_gzip_static_module中的gzip相关配置参数。 gzip 名称 默认配置 作用域 官方说明 中文解读 模块 gzip gzip off; http, server, location, if in location Enables or disables gzipping of ...

    付永刚 评论0 收藏0
  • nginx lua api解读

    摘要:对于需要进一步注意的是参数的使用,可以传入所定义的所有的状态码常量如等和两个模块内核常量只支持和这两个,如果传入其他的如等则进程住。 序 本文主要解读下nginx lua module的主要方法和api。 ngx_lua运行阶段 showImg(https://segmentfault.com/img/bVHFqI?w=1005&h=910); initialization phase...

    shery 评论0 收藏0
  • nginx http模块配置参数解读

    摘要:名称默认配置作用域官方说明中文解读模块是否开启对后端的缓冲指定一个连接到代理服务器的超时时间,单位为秒,需要注意的是这个时间最好不要超过秒。 序 本文主要解析一下nginx http模块配置参数。主要分socket相关参数,对clinet请求的buffer参数以及对response的buffer参数。 socket 名称 默认配置 作用域 官方说明 中文解读 模块 sendf...

    pf_miles 评论0 收藏0
  • Nginx 内容缓存及常见参数配置

    摘要:设置一个共享内存区,该内存区用于存储缓存键和元数据,有些类似计时器的用途。注意,非活动内容有别于过期内容。在两次缓存管理器启动的间隔,缓存的数据量可能短暂超过配置的大小。为在缓存响应之前必须使用相同密钥的请求的最小次数。 原文链接:何晓东 博客 使用场景:项目的页面需要加载很多数据,也不是经常变化的,不涉及个性化定制,为每次请求去动态生成数据,性能比不上根据请求路由和参数缓存一下结果,...

    KnewOne 评论0 收藏0
  • Nginx 内容缓存及常见参数配置

    摘要:设置一个共享内存区,该内存区用于存储缓存键和元数据,有些类似计时器的用途。注意,非活动内容有别于过期内容。在两次缓存管理器启动的间隔,缓存的数据量可能短暂超过配置的大小。为在缓存响应之前必须使用相同密钥的请求的最小次数。 原文链接:何晓东 博客 使用场景:项目的页面需要加载很多数据,也不是经常变化的,不涉及个性化定制,为每次请求去动态生成数据,性能比不上根据请求路由和参数缓存一下结果,...

    olle 评论0 收藏0

发表评论

0条评论

alanoddsoff

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<