栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx 缓存

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx 缓存

nginx的缓存机制

proxy模块指令

配置块名称名称
httpproxy_cache_path指定缓冲区的路径
levels缓存目录级最高三层,每层1-2个字符表示,比如:1:1:2 三层
keys_zone 缓存块名称及内存块大小,比如:cache_item:500m表示声明一个名为cache_item大小为500m,超出大小后最早的被清除
max_size缓存区硬盘的最大值,超出闲置数据将被清除
inactive最长闲置时间,比如:10d ,表示一个数据被闲置10天则将被清除
locationproxy_cache指定缓冲区,对应keys_zone中的设定的值
proxy_cache_key通过参数拼装参数key如: h o s t host hosturi i s a r g s is_args isa​rgsargs 则会已全部md5值作为key
proxy_cache_valid对不同的状态码设置缓存有效期

192.168.34.165

nginx.conf加入下面的代码:
proxy_cache_path /cache/nginx levels=1:2 keys_zone=cache_filename:10m max_size=5g inactive=60m use_temp_path=off;

注:位置在server外面

/vhost/study_nginx.conf

server{
   listen       80;
   server_name  www.study_nginx.com;
   root   /www/study_nginx;
   index index.html index.htm;

   location /api/ { 
      proxy_cache test_cache;
      proxy_pass http://www.nginx_cache.com:8080/api/;
      proxy_cache_valid 200 304 12h;
      proxy_cache_valid any 10m;
      proxy_cache_key $host$uri$is_args$args;
      include proxy_params;
    } 
    location ~ /clear_cache(.*) {
     allow   all;
     proxy_cache_purge test_cache $host$1$is_args$args;
    }
}

192.168.34.135【代理服务器】

/vhost/nginx_cache.conf

server{
   listen       8080;
   server_name  www.nginx_cache.com;
   root   /www/nginx_cache;
   index index.html index.htm;

   location ~ .php$ {
       root           /www/nginx_cache;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param script_FILENAME $document_root$fastcgi_script_name ;
       include        fastcgi_params;
   }
}

/www/nginx_cache/api/index.php


访问:http://www.study_nginx.com/api/index.php

查看缓存文件【/cache/nginx/e/2b】

修改/www/nginx_cache/api/index.php 内容。再次访问:http://www.study_nginx.com/api/index.php。

发现还是上面的页面内容。缓存成功。

清除缓存

ngx_cache_purge介绍
若清除缓存时出现异常:

12180#0: unknown directive "proxy_cache_purge" in /vhost/blog.conf:24:

则表明缺失这个模块,nginx需重新编译,如下:

ngx_cache_purge是nginx的第三方模块,能够帮助我清除nginx中的缓存。

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.1.1g --add-module=/home/ngx_cache_purge-2.3

make -j2

make instal

配置/vhost/study_nginx.conf

location ~ /clear_cache(.*) {
     allow   all;
     proxy_cache_purge cache_filename $host$1$is_args$args;
}
#cache_name 需清除的缓存文件名

访问:http://www.study_nginx.com/clear_cache/api/index.php

再次访问:http://www.study_nginx.com/api/index.php。

发现页面显示了新修改的内容,缓存已删除。

控制nginx缓存

/vhost/study_nginx.conf

 set $a 0;
 if ( $request_uri ~ .php$) {
      set $a 1;
 }
 proxy_no_cache $a;


先访问:http://www.study_nginx.com/clear_cache/api/index.php
删除缓存文件。
再次访问:http://www.study_nginx.com/api/index.php

然后查看是否生成缓存文件。

proxy_no_cache
参数中的值可以设置多个,但是多个值中,只要有一个是不为0的,就不会缓存数据。

nginx浏览器缓存

/vhost/study_nginx.conf

 location  {
        expires 1m;
   }

访问:http://www.study_nginx.com/

发现第2次访问之后状态为304。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/333596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号