首页 > 系统相关 >Nginx基础 - 07代理缓存

Nginx基础 - 07代理缓存

时间:2023-03-11 17:34:15浏览次数:32  
标签:缓存 http 07 Cache cache Nginx proxy

 

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

一、缓存常见类型

服务端缓存: 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

 

标签:缓存,http,07,Cache,cache,Nginx,proxy
From: https://www.cnblogs.com/kingdomer/p/13937967.html

相关文章

  • go学习 day207 继承
    编写一个学生考试系统packagemainimport( "fmt")//编写一个学生考试系统typestudentstruct{ Namestring Ageint Scoreint}//将Pupil和Graduate......
  • Nginx如何升级Openssl
    1.什么是Openssl?在计算机网络上,OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信,避免窃听,同时确认另一端连线者的身份。这个包广泛被应用在互联......
  • leetcode-1071-easy
    GreatestCommonDivisorofStringsFortwostringssandt,wesay"tdividess"ifandonlyifs=t+...+t(i.e.,tisconcatenatedwithitselfoneormor......
  • Nginx基础 - 04静态资源
      一、静态资源类型Nginx作为静态资源Web服务器部署配置,传输非常的高效,常常用于静态资源处理、请求、动静分离。非服务器动态运行生成的文件属于静态资源。类型......
  • Nginx基础 - 03基本配置
     一、Nginx配置文件结构Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件。整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始于......
  • Nginx 负载均衡反向代理 Windows安装部署教程
     一、Nginx简介   Nginx(enginex)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。它也是一种轻量级的Web服务器,可以作为独立的服务器部署网站......
  • ES007-Elasticsearch中文分词集成
    1、elasticsearch官方只提供smartcn这个中文分词插件,效果不是很好2、引入分词器前命令行下测试curl'http://localhost:9200/jf/_analyze?pretty=true'-d'{......
  • 【LeetCode回溯算法#07】子集问题I+II,巩固解题模板并详解回溯算法中的去重问题
    子集力扣题目链接给你一个整数数组nums,数组中的元素互不相同。返回该数组所有可能的子集(幂集)。解集不能包含重复的子集。你可以按任意顺序返回解集。示例1:输......
  • C# 调用 c++ DLLL试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B
     网上各种设置目标编译平台为x86都解决不了问题,有可能是DLL依赖的文件的位数有问题1、先查一下被调用的DLL用了那些DLLvs的开发人员工具控制台输入 dumpbin/depe......
  • 精通api,07文件目录
    DeleteFile,CopyFile,MoveFile来完成文件的删除,复制和移动功能.带Ex为增强版.整主(整参个数,p符参值[]){//-d参数,删除文件.如(0==长比较串("-d",参值[1])......