惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
小众软件
小众软件
美团技术团队
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
D
Docker
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
雷峰网
雷峰网
The Cloudflare Blog

齐物 on 老刘博客

Linux 人脸识别登录折腾记:Howdy + Arch Linux 实战 Arch Linux 电源优化指南 Ubuntu 26.04 用了一个月 告别 PaperMod:审美疲劳后的 Hugo 博客“换装”记 Ubuntu25.10编译安装Ibus-libpinyin 博客托管平台换成edgeone pages 博客评论换回Giscus EndeavourOS + Btrfs + Snapper + GRUB:完整的快照回滚系统配置指南 把GotoSocial嵌入到Hugo 给Hugo PaperMod主题添加一个漂亮又简洁的友情链接页面 群晖NAS安装FRP客户端(frpc) Ubuntu24.04安装Windterm 从 Windows 到 Linux:重新体验的过程与心得 解决腾讯 EdgeOne 域名解析与 SSL 证书申请失败问题 折腾杀毒软件的一点感想 开箱|媳妇的华为 Watch Fit 4 Ghost 博客与 Mailgun 邮件服务申请全流程 Ubuntu部署ghost博客所踩的小坑 当GitHub二次验证丢失:我的完整恢复经历 在群晖里安装简单的笔记软件flatnotes 手机上有好用的输入法吗? 再次使用 KeepassXC进行密码管理 解决Windows11打开安全中心空白的方法 修汽车空调 给Hugo PaperMod增加giscus评论系统 2024年,高德地图和百度地图选哪个? 电视和投影仪,哪个对孩子的眼睛更好? 使用Fcitx5 for Android,读《万历十五年》 2024年 Web 开发趋势 利用cloudflare页面规则设置301永久跳转
WordPress开启Nginx fastcgi_cache缓存加速
老刘 · 2019-09-08 · via 齐物 on 老刘博客

昨天趁着重装景安服务器,把一直想弄的 nginx fastcgi_cache 配置好了,虽然走了不少弯路,但最终成功了,网页打开速度明显提升。


一、安装 Nginx ngx_cache_purge 模块

由于使用的是 OneinStack 面板,以下步骤基于 OneinStack。其他面板如 LNMP 可能需要微调。

  1. 检查 ngx_cache_purge 是否安装
1
nginx -V 2>&1 | grep -o ngx_cache_purge

如果没有输出,表示未安装。

  1. 下载并解压 ngx_cache_purge 模块
1
2
3
cd /root/oneinstack/src
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
  1. 解压 Nginx 及依赖包
1
2
3
4
tar xzf nginx-1.16.1.tar.gz
tar xzf pcre-8.43.tar.gz
tar xzf openssl-1.1.1c.tar.gz
cd /root/oneinstack/src/nginx-1.16.1
  1. 查看 Nginx 编译参数
  1. 编译并安装 ngx_cache_purge

注意:添加模块时必须保留原有编译参数,只在末尾加上 --add-module=../ngx_cache_purge-2.3

1
2
3
4
5
6
7
8
9
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-http_stub_status_module --with-http_v2_module --with-http_ssl_module \
--with-http_gzip_static_module --with-http_realip_module --with-http_flv_module \
--with-http_mp4_module --with-openssl=../openssl-1.0.2o --with-pcre=../pcre-8.42 \
--with-pcre-jit --with-ld-opt=-ljemalloc --add-module=../ngx_cache_purge-2.3

make
mv /usr/local/nginx/sbin/nginx{,_`date +%F`} # 备份旧版本
cp objs/nginx /usr/local/nginx/sbin
  1. 验证安装成功
1
nginx -V 2>&1 | grep -o ngx_cache_purge

输出 ngx_cache_purge 表示安装成功。


二、Nginx 开启 FastCGI Cache 配置实例

以下为我的 WordPress 网站配置示例(www.yanjingweb.cn):

 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
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;

server {
    listen 80;
    listen 443 ssl http2;
    
    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;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache WORDPRESS;
        fastcgi_cache_valid 200 301 302 1d;

        add_header X-Cache "$upstream_cache_status From $host";
        add_header Nginx-Cache "$upstream_cache_status";
    }

    # 缓存清理配置(可选)
    location ~ /purge(/.*) {
        allow 127.0.0.1;
        allow 122.115.122.111; # 请改为你服务器 IP
        deny all;
        fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
    }
}

三、测试与验证

  1. 检查 Nginx 配置是否正确:
  1. 重启 Nginx:
  1. 打开网页,F5 刷新几次,查看浏览器响应头:

如果看到 X-Cache: HIT 字样,表示缓存生效。

  1. 查看缓存文件夹 /tmp/wpcache,可以看到缓存文件生成。

四、效果总结

开启 fastcgi_cache 后,即使不使用 CDN,网页打开速度明显提升,实现了秒开体验。

有动手能力的同学可以尝试,操作难度中等,但效果非常明显。