Nginx fastcgi_cache介绍

说到wordpress的缓存,大家想到的肯定是 WP-Super-Cache 的静态html缓存,

以及 memcachedredis 动态缓存,插件的缓存效果肯定是有的,

但是容易出现各种问题,比如配置很复杂、配置全英文、插件之间的冲突等,

所以今天我要教大家一个更高级的缓存:Nginx fastcgi_cache缓存,

直接在nginx层面缓存页面,还支持缓存伪静态!效果比起传统的php缓存好得太多了,

因为很多人使用宝塔面板,所以今天的教程是基于宝塔面板的教程。

全部配置好之后,别忘了重载Nginx设置,缓存就加好了

来到宝塔后台,在软件商店找到Nginx,在配置修改中添加以下内容:

(大概在67行下面粘贴即可)

1
2
3
4
5
6
7
8
# 启用fastcgi_cache 开始
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/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;
# 启用fastcgi_cache 结束

Nginx配置

完整的nginx配置,可以一键复制使用,适合nginx1.18以上,没有任何问题

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

stream {
    log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
  
    access_log /www/wwwlogs/tcp-access.log tcp_format;
    error_log /www/wwwlogs/tcp-error.log;
    include /www/server/panel/vhost/nginx/tcp/*.conf;
}

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
		#include luawaf.conf;

		include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

        gzip on;
        gzip_static on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

        # 启用fastcgi_cache 开始
		
        fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
        fastcgi_temp_path /tmp/wpcache/temp;
        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        fastcgi_cache_use_stale error timeout invalid_header http_500;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
		
        # 启用fastcgi_cache 结束
server
    {
        listen 888;
        server_name phpmyadmin;
        index index.html index.htm index.php;
        root  /www/server/phpmyadmin;

        #error_page   404   /404.html;
        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }
        
        access_log  /www/wwwlogs/access.log;
    }
include /www/server/panel/vhost/nginx/*.conf;
}

网站设置

在宝塔后台的网站列表中,找到你的网站,点击配置文件,将以下代码添加到配置文件中去:

注意点:php-cgi-74.sock 版本

假如你用的是php7.2版本,那么就是

php-cgi-72.sock  改成你的php版本即可

大概在第7行下面粘贴即可,如 root /www/wwwroot/xxx.com 的后面

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#启用fastcgi_cache 开始

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/|/xmlrpc.php|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;
}
#这里请参考你网站之前的配置,特别是sock的路径,弄错了就502了!如果你的网站使用PHP7.4,就写-74.sock
location ~ \.php$ {

try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi-74.sock;
fastcgi_index index.php;
include fastcgi.conf; 
add_header Strict-Transport-Security "max-age=1";
    
#新增的缓存规则
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Cache "$upstream_cache_status From $host";
fastcgi_cache WORDPRESS;
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_valid 200 301 302 1d;
}
#缓存清理配置
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}

#启用fastcgi_cache 结束

全部配置好之后,别忘了重载Nginx设置生效,缓存就加好了

判断缓存状态

F12 访问网站首页,查看文件头,

如果出现HIT则是缓存了, BYPASS 则是因设置原因未缓存, MISS即这个页面还没被缓存,新发布或刚被删除的页面,首次访问将出现这个状态,如图所示:

清理缓存

将以下代码添加到面板定时任务即可(shell脚本)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

# Path to the nginx cache directory
NGINX_CACHE_PATH="/tmp/wpcache"

# Function to clear the nginx cache
clear_cache() {
    echo "Clearing nginx cache..."
    sudo rm -rf $NGINX_CACHE_PATH/*
    echo "Nginx cache cleared."
}

# Function to reload nginx
reload_nginx() {
    echo "Reloading Nginx..."
    sudo /etc/init.d/nginx reload
    echo "Nginx reloaded."
}

# Clear the cache
clear_cache

# Reload Nginx
reload_nginx