介绍
ngx_cache_purge
is nginx
module which adds ability to purge content from FastCGI
, proxy
, SCGI
and uWSGI
caches.
开始安装
准备安装包
安装包下载地址:https://github.com/FRiCKLE/ngx_cache_purge/releases
[root@master nginx-1.18.0]# cd /usr/src/
[root@master src]# wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
[root@master src]# tar zxf 2.3.tar.gz
查看nginx安装信息
[root@master nginx-1.18.0]# /usr/local/nginx/sbin/nginx -V
nginx version: WebSite/9.9.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --modules-path=/usr/local/nginx/modules --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --pid-path=/usr/local/nginx/tmp/nginx.pid --lock-path=/usr/local/nginx/tmp/nginx.lock --user=nginx --group=nginx --with-threads --with-file-aio --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E
重编译Nginx
添加如下:
- –add-module=/usr/src/ngx_cache_purge-2.3
[root@master nginx-1.18.0]# ./configure --prefix="${Nginx_Path}" --sbin-path="${Nginx_Path}/sbin/nginx" --modules-path="${Nginx_Path}/modules" --conf-path="${Nginx_Path}/conf/nginx.conf" --error-log-path="${Nginx_Path}/logs/error.log" --pid-path="${Nginx_Path}/tmp/nginx.pid" --lock-path="${Nginx_Path}/tmp/nginx.lock" --user="${Nginx_User}" --group="${Nginx_User}" --with-threads --with-file-aio --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-http_sub_module --add-module=/usr/src/ngx_cache_purge-2.3 --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt="-Wl,-E" --with-openssl=/usr/src/openssl1.1.1g/
[root@master nginx-1.18.0]# make
等待无报错后停止nginx并备份原Nginx,拷贝文件
[root@master nginx-1.18.0]# cp /usr/local/nginx/sbin/nginx{,.bak}
[root@master nginx-1.18.0]# cp objs/nginx /usr/local/nginx/sbin/nginx
检查安装信息
[root@master sbin]# /usr/local/nginx/sbin/nginx -V
nginx version: WebSite/9.9.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --modules-path=/usr/local/nginx/modules --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --pid-path=/usr/local/nginx/tmp/nginx.pid --lock-path=/usr/local/nginx/tmp/nginx.lock --user=nginx --group=nginx --with-threads --with-file-aio --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-http_sub_module --add-module=/usr/src/ngx_cache_purge-2.3 --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E
修改Nginx配置文件
在nginx.conf配置文件中,http{}中添加如下内容:
#指定缓存目录(需提前创建),缓存层数,keys名
fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=500m;
fastcgi_temp_path /dev/shm/nginx-cache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
#忽略一切nocache申明,避免不缓存伪静态等
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
在Server{}合适的位置中添加(请根据实际情况修改)如下内容:
server
{
#Conf-Start
#listen 80;
listen 443 ssl http2;
server_name www.lolicp.com lolicp.com;
index index.html index.htm index.php;
root /wwwroot/blog_lolicp.com;
charset utf-8;
#Conf-END
-------此处省略!-------
if (!-e $request_filename) {
rewrite . /index.php last;
}
set $skip_cache 0;
#post访问不缓存
if ($request_method = POST) {
set $skip_cache 1;
}
#动态查询不缓存
if ($query_string != "") {
set $skip_cache 1;
}
#后台等特定页面不缓存(其他需求请自行添加即可)
if ($request_uri ~* "/wp-admin/|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
#对登录用户、评论过的用户不展示缓存
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
#PHP-INFO-START PHP引用配置,可以注释或修改
location ~ [^/]\.php(/|$) {
if ($query_string ~* ^(.*)author=(.*)$){
rewrite ".*" https://$host/waf_auth break;
}
root /wwwroot/blog_lolicp.com;
fastcgi_pass WebStie_php_Server;
#fastcgi_pass unix:/dev/shm/php-fpm.sock/sock_blog1;
fastcgi_index index.php;
include fastcgi.conf;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
#新增的缓存规则
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Cache "$upstream_cache_status From $host";
add_header Cache-Control max-age=0;
add_header Nginx-Cache "$upstream_cache_status";
add_header Last-Modified $date_gmt;
add_header X-Frame-Options SAMEORIGIN; # 只允许本站用 frame 来嵌套
add_header X-Content-Type-Options nosniff; # 禁止嗅探文件类型
add_header X-XSS-Protection "1; mode=block"; # XSS 保护
etag on;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 1d;
}
-------!此处省略-------
error_log /usr/local/nginx/logs/www.lolicp.com.error.log;
}
安装Nginx Helper插件
在WordPress插件中直接搜索Nginx Helper 安装并启用插件