nginx安装第三方ngx_cache_purge模块,purge命令清除静态缓存

原已经安装好的nginx,现在需要添加一个未被编译安装的模块

举例说明:安装第三方的ngx_cache_purge模块(用于清除指定URL的缓存)

1、下载模块包,进行解压,记住解压包的位置,添加时要用到。

2、查看nginx编译安装时的命令,安装了哪些模块

命令 /usr/local/nginx/sbin/nginx -V
在这里插入图片描述
configure arguments后边的内容为nginx的路径以及已经安装的模块。

3.加入需要安装的模块,--add-module=/usr/local/ngx_cache_purge-1.2

进入到nginx源码包目录下输入以下命令,进行安装

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/local/ngx_cache_purge-1.2

输入make进行编译,千万不要make install因为会覆盖原来已经安装好的内容,另外,编译必须没错误才行。

输入/usr/local/nginx/sbin/nginx -V,查看ngx_cache_purge是否安装成功。
在这里插入图片描述
4、停止nginx服务,替换nginx二进制文件

 #替换前最好先备份一下原来的文件
 cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
 
 #把重新编译好的objs/nginx文件 复制到/usr/local/nginx/sbin/ 下,替换原来的nginx文件
 cp objs/nginx  /usr/local/nginx/sbin/nginx

5、配置nginx.conf文件

在已经配置好的server外加入以下内容:

#cache begin
  proxy_buffering on;
  proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=my-cache:150m max_size=300m inactive=1d;
  proxy_temp_path /usr/local/nginx/nginx_temp;
  proxy_buffer_size 16k;
  proxy_buffers 4 64k;
  proxy_busy_buffers_size 128k;
  proxy_temp_file_write_size 128k;
  #cache end

重要参数说明:proxy_cache_path/usr/local/nginx/cache缓存的文件目录,

levels=1:2表示缓存文件两级目录,1表示第一级目录名为1位数,2表示第二级目录名为2位数

keys_zone=my-cache:150m max_size=300m inactive=1d缓存区域名字,分配150m空间,最大缓存300m,有效期1天

在已经配置好的server内加入以下内容:

location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
    
    
                proxy_set_header       Host $host;
                proxy_pass      http://101.118.11.20:9090;
                proxy_redirect http:// $scheme://;
                add_header X-Cache $upstream_cache_status;
                proxy_cache my-cache;#对应前边的keys_zone=my-cache
                proxy_cache_valid 200 12h;
                proxy_cache_valid 301 302 1m;
                proxy_cache_key $host$uri$is_args$args;
      }
 
location ~ /purge(/.*) {
    
    
                  allow all;#指定可以清除缓存的ip,all允许所有ip清除
                  proxy_cache_purge my-cache $host$1$is_args$args;
}

6、启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

7、purge命令清除静态缓存

http://域名+purge+静态资源相对路径 来清除静态资源缓存

例如,在浏览器地址栏输入
http://www.baidu.com/purge/resources/app/css/product.css

即可清除在nginx服务器缓存的静态文件

参考:文章

猜你喜欢

转载自blog.csdn.net/chj_1224365967/article/details/121167109
今日推荐