nginx 07-Nginx缓存服务

Nginx缓存服务配置语法

proxy_cache_path path kes_zone=name:size 
[levels=levels] 
[use_temp_path=on|off]
[inactive=time]
[max_size=size]
[manager_files=number]
[manager_sleep=time]
[manager_threshold=time]
[loader_files=number]
[loader_sleep=time]
[loader_threshold=time]
[purger=on|off]
[purger_files=number]
[purger_sleep=time]
[purger_threshold=time]
(http)
proxy_cache zone|off;
(http、server、location)
proxy_cache_valid [code..] time;
(http、server、location)
作用:缓存过期周期
proxy_cache_key string;
(http、server、location)
作用:缓存维度,默认:proxy_cache_key $scheme$proxy_host$request_uri

Nginx缓存服务配置案例

http{
    upstream test {
        server 192.168.10.10:8080 down;
        server 192.168.10.10:8081 max_fails=1 fail_timeout=10s;
        server 192.168.10.11:8080 backup;
    }
    
    proxy_cache_path /opt/app/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off;
    server {
        listen 80;
        server_name location;
        location / {
        proxy_cache off;
            proxy_pass http://test;
            proxy_cache_valid 200 304 12h;
            proxy_cache_valid any 10m;
            proxy_cache_key $host$uri$is_args$args;
            add_header  Nginx-Cache "$upstream_cache_status";  
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        }
    }
}

如何清理指定缓存

  • 方法1:rm -rf 缓存目录内容
  • 方法2:第三方扩展模块ngx_cache_purge

如何让部分页面不缓存

  • 配置语法:
proxy_no-cache string ...;
(http、server、location)
  • 配置案例:
server {
    listen 80;
    server_name location;
    
    if ($request_uri ~ ^/(url3|login|register|password\/reset)) {
        set $cookie_nocche 1;
    }
    location / {
    proxy_cache off;
        proxy_pass http://test;
        proxy_cache_valid 200 304 12h;
        proxy_cache_valid any 10m;
        proxy_cache_key $host$uri$is_args$args;
        proxy_no_cache $cookie_nocche $arg_nocache $arg_comment;
        proxy_no_cache $http_pragma $http_authorization;
        add_header  Nginx-Cache "$upstream_cache_status";  
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    }
}

大文件分片请求

  • 配置语法:
slice size;
(http、server、location) 默认:slice 0
  • 优势:

        每个子请求收到的数据都会形成一个独立文件,一个请求断了,其他请求不受影响
  • 缺点:

        当文件很大或者slice很小的时候,可能会导致文件描述符耗尽等情况

转载于:https://www.cnblogs.com/liangjingfu/p/10677108.html

你可能感兴趣的:(nginx 07-Nginx缓存服务)