zoukankan      html  css  js  c++  java
  • 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很小的时候,可能会导致文件描述符耗尽等情况
  • 相关阅读:
    有关 PHP 和 MySQL 时区的一点总结
    PHP CLI模式下的多进程应用
    Linux编程之:五个常见PHP数据库问题
    用php定制404错误页面 并发信通知管理员
    配置PHP站点安全综合教程
    新手必看的PHP学习入门的一些基础知识
    彻底杜绝PHP的session cookie错误
    专家预言:PHP将比Java更受开发人员欢迎
    PHP企业级应用之WebService续篇
    清除 数据库 日志 以 Db_Test 为例
  • 原文地址:https://www.cnblogs.com/liangjingfu/p/10677108.html
Copyright © 2011-2022 走看看