博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Nginx基础 - 07代理缓存

Posted on 2023-03-11 17:24  Kingdomer  阅读(120)  评论(0编辑  收藏  举报

 

缓存是用来减少后端压力,将压力尽可能的往前推, 提高网站的并发延时。

一、缓存常见类型

服务端缓存: redis/memcached

代理缓存,获取服务端内容进行缓存: Nginx_proxy  

客户端浏览器缓存

 

二、缓存配置语法

 

proxy_cache配置语法

Syntax:    proxy_cache zone | off;
Default:proxy_cache off;
Context: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.

 

缓存路径

Syntax:  proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] 
[max_size=size] [min_free=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]; Default: — Context: 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.
    For example, in the following configuration   proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
    file names in a cache will look like this:    /data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c

A cached response is first written to a temporary file, and then the file is renamed.
  Starting from version 0.8.9, temporary files and the cache can be put on different file systems.
  However, be aware that in this case a file is copied across two file systems instead of the cheap renaming operation.
  It is thus recommended that for any given location both cache and a directory holding temporary files are put on the same file system.
The directory for temporary files is set based on the use_temp_path parameter (1.7.10).
If this parameter is omitted or set to the value on, the directory set by the proxy_temp_path directive for the given location will be used.
If the value is set to off, temporary files will be put directly in the cache directory.

In addition, all active keys and information about data are stored in a shared memory zone, whose name and size are configured by the keys_zone parameter. 
  One megabyte zone can store about 8 thousand keys.

Cached data that are not accessed during the time specified by the inactive parameter get removed from the cache regardless of their freshness. 
  By default, inactive is set to 10 minutes.

The special "cache manager" process monitors the maximum cache size set by the max_size parameter, 
 and the minimum amount of free space set by the min_free (1.19.1) parameter on the file system with cache.
When the size is exceeded or there is not enough free space, it removes the least recently used data.
The data is removed in iterations configured by manager_files, manager_threshold, and manager_sleep parameters (1.11.5).
During one iteration no more than manager_files items are deleted (by default, 100).
The duration of one iteration is limited by the manager_threshold parameter (by default, 200 milliseconds).
Between iterations, a pause configured by the manager_sleep parameter (by default, 50 milliseconds) is made.

A minute after the start the special "cache loader" process is activated.
   It loads information about previously cached data stored on file system into a cache zone.
The loading is also done in iterations. During one iteration no more than loader_files items are loaded (by default, 100).
Besides, the duration of one iteration is limited by the loader_threshold parameter (by default, 200 milliseconds).
Between iterations, a pause configured by the loader_sleep parameter (by default, 50 milliseconds) is made.

 

缓存过期周期 

Syntax:  proxy_cache_valid [code ...] time;
Default: —
Context: http, server, location

 

Sets caching time for different response codes. For example, the following directives
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;
        set 10 minutes of caching for responses with codes 200 and 302 and 1 minute for responses with code 404.
        If only caching time is specified  proxy_cache_valid 5m; then only 200, 301, and 302 responses are cached.

In addition, the any parameter can be specified to cache any responses:
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 301 1h;
    proxy_cache_valid any 1m;

Parameters of caching can also be set directly in the response header. This has higher priority than setting of caching time using the directive.

 

缓存维度

Syntax:  proxy_cache_key string;
Default: proxy_cache_key $scheme$proxy_host$request_uri;
Context: http, server, location

Defines a key for caching, for example                       proxy_cache_key  "$host$request_uri $cookie_user";
By default, the directive’s value is close to the string     proxy_cache_key  $scheme$proxy_host$uri$is_args$args;

 

三、缓存配置实践

[root@my-node51 nginx]# for i in {1..3}; do echo Shop1-Url$i > ./shop1/shop$i.html; done
[root@my-node51 nginx]# for i in {1..3}; do echo Shop2-Url$i > ./shop2/shop$i.html; done
[root@my-node51 nginx]# for i in {1..3}; do echo Shop3-Url$i > ./shop3/shop$i.html; done

 

proxy_cache_path /application/nginx/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;

    location / {
        proxy_pass http://nginxserver;
        proxy_cache code_cache;
        proxy_cache_valid 200 304 12h;
        proxy_cache_valid any 10m;
        add_header Nginx-Cache "$upstream_cache_status";
        proxy_next_upstream error timeout invalid_header http_500;
    }

 

proxy_cache_path 存放缓存的临时文件

levels 按照两层目录分级

keys_zone 开辟空间名, 10m: 开辟空间大小, 1m可存放8000个key

max_size 控制最大大小, 超过后Nginx会启用淘汰规则

inactive 60分钟没有被访问缓存会被清理

use_temp_path 临时文件, 会影响性能, 建议关闭

 

proxy_cache 开启缓存

proxy_cache_valid 200 304 12h; 状态码200|304的过期时间为12小时, 其他状态码的过期时间为10分钟

proxy_chche_key 缓存key

add_header 增加头信息, 观察客户端response是否命中

proxy_next_upstream 出现502-504或错误, 会跳过此台服务器访问下一个服务器

 

缓存日志 

    log_format cache_main '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" "$upstream_cache_status"';

server {
    listen 82;
    server_name 192.168.6.10;
    access_log /var/log/nginx/proxy_cache.log cache_main;
    ......
}

 

192.168.6.102 - - [08/Mar/2023:21:46:16 +0800] "GET /shop1.html HTTP/1.1" 200 11 "-" 
                  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "-" "MISS"

192.168.6.102 - - [08/Mar/2023:21:48:29 +0800] "GET /shop1.html HTTP/1.1" 200 11 "-" 
                  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "-" "HIT"

 

       

 

[root@my-node10 cache]# pwd
/application/nginx/cache

[root@my-node10 cache]# tree
.
├── 4
│   └── f1
│       └── e485067efd63966d8db894d1b7bdaf14
└── 9
    └── b9
        └── 6b819b883e00e57216d3f4c5662ccb99

4 directories, 2 files

  

四、清理缓存

手动清理

[root@my-node10 cache]# rm -rf *
[root@my-node10 cache]# curl -s -I http://192.168.6.10:82/shop3.html | grep "Nginx-Cache"
Nginx-Cache: MISS
[root@my-node10 cache]# curl -s -I http://192.168.6.10:82/shop3.html | grep "Nginx-Cache"
Nginx-Cache: HIT

三方扩展模块ngx_cache_purge

访问http://192.168.6.10:81/purge/shop1.html

location ~ /purge(/.*) {
    proxy_cache_purge  code_cache  $host$1$is_args$args;
}

  

五、如何让部分页面不缓存

    if ($request_uri ~ ^/(shop3|login|register|password)) {
        set $cookie_nocache 1;
    }

    location / {
        proxy_pass http://nginxserver;
        proxy_cache code_cache;
        proxy_cache_valid 200 304 12h;
        proxy_cache_valid any 10m;
        add_header Nginx-Cache "$upstream_cache_status";
        proxy_next_upstream error timeout invalid_header http_500;
        proxy_no_cache $cookie_nocache $arg_nocache$arg_comment;
        proxy_no_cache $http_pragma $http_authorization;
    }

  

[root@my-node10 cache]# rm -rf *
[root@my-node10 cache]# curl -s -I http://192.168.6.10:82/shop3.html | grep "Nginx-Cache"
Nginx-Cache: MISS
[root@my-node10 cache]# curl -s -I http://192.168.6.10:82/shop3.html | grep "Nginx-Cache"
Nginx-Cache: MISS
[root@my-node10 cache]# curl -s -I http://192.168.6.10:82/shop3.html | grep "Nginx-Cache"
Nginx-Cache: MISS

[root@my-node10 cache]# curl -s -I http://192.168.6.10:82/shop2.html | grep "Nginx-Cache"
Nginx-Cache: MISS
[root@my-node10 cache]# curl -s -I http://192.168.6.10:82/shop2.html | grep "Nginx-Cache"
Nginx-Cache: HIT

  

缓存额外知识

awk '{if($NF==""HIT"") hit++} END {printf "%.2f%", hit/NR}' access.log

通过crontab脚本将每天的命中率统计到一个日志中,以备查看。

1 0 * * * /opt/shell/nginx_cache_hit >> /usr/local/nginx/logs/hit