Proxy缓存
缓存类型
- 网页缓存 (公网)CDN
- 数据库缓存 memcache redis
- 网页缓存 nginx-proxy
- 客户端缓存 浏览器缓存
模块
- ngx_http_proxy_module
语法
缓存开关
Syntax: 	    proxy_cache zone | off;
Default: 	    proxy_cache off;
Context: 	http, server, location代理缓存
Syntax: 	proxy_cache_path path [levels=levels] 			keys_zone=name:size[inactive=time] [max_size=size] 			[manager_files=number]
Default:  —
Context: http
example:proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;缓存维度
Syntax: 	    proxy_cache_key string;  定义缓存唯一key,通过唯一key来进行hash存取,缓存文件名
Default: 	    proxy_cache_key $scheme$proxy_host$request_uri;
Context: 	http, server, location缓存过期
Syntax: 	    proxy_cache_valid [code ...] time;
Default: 	    —
Context: 	http, server, location
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404      1m;
启动缓存
1 延续代理实验
2 设置nginx-2为缓存服务器
- vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;gzip  on;include /etc/nginx/conf.d/*.conf;#开启反向代理缓存proxy_cache_path /app/limou/cache levels=1:2 keys_zone=proxy_cache:10m max_size=10g inactive=2h use_temp_path=off;
}-  proxy_cache_path命令中的参数及对应配置说明如下: 1、proxy_cache_path /app/laochen/cache:指定了缓存文件存储的路径为 /app/laochen/cache。2、levels=1:2:设置了缓存目录的层级结构。这里 levels=1:2 表示在缓存目录下,使用两个级别的子目录来存储缓存文件。第一级目录有 1 个字符(例如 A),第二级目录有 2 个字符(例如 00),这种结构有助于管理大量缓存文件,避免单个目录中文件过多。3、keys_zone=proxy_cache:10m:定义了一个名为 proxy_cache 的共享内存区域,用于存储缓存键的元数据(例如缓存的路径、过期时间等)。10m 表示这个内存区域的大小为 10 兆字节。4、max_size=10g:设置了缓存的最大总大小为 10 GB。超过这个大小的缓存会被清理,以保持总缓存大小在限制之内。5、inactive=60m:定义了缓存的过期时间。如果缓存项在 60 分钟内没有被访问,它将被标记为过期并最终被清理。6、use_temp_path=off:表示缓存的临时文件不使用临时路径。默认情况下,Nginx 会在写入缓存文件时先使用临时文件,如果设置为 off,则直接写入最终缓存路径。
-  vim /etc/nginx/conf.d/default.conf 
server {listen       80;server_name  localhost;access_log  /var/log/nginx/host.access.log  main;location / {root   /usr/share/nginx/html;index  index.html index.htm;#开启反向代理缓存# Proxy_cache   使用名为 的对应缓存配置proxy_cache proxy_cache;# proxy_cache_valid  200 206 304 301 302 12h;# 对http状态码为200、304…的内容缓存12小时proxy_cache_valid 200 304 12h;# 设置不同相应码的缓存时间,除了上面配置12小时的,其他的的存10分钟proxy_cache_valid any 10m;# proxy_cache_key $uri  定义缓存唯一key,通过唯一key来进行hash存取proxy_cache_key $host$uri$is_args$args;# add_header:判断数据包是否缓存了该信息#缓存命中情况如何在http头中体现,以及在nginx日志中查看add_header  Nginx-Cache "$upstream_cache_status";# proxy_next_upstream 出现502-504或错误,会跳过此台服务器访问下一台服务器proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}
}-  -  mkdir -p /app/limou/cache - 准备缓存文件的存放目录
 
-  systemctl restart nginx - 重启服务器
 
 
-  
3 使用PC客户机,再次访问nginx-2服务器
4 通过PC客户机浏览器开发者功能。观察是否命中缓存。
- 未命中miss

- 命中hit
