> ## Content Index
> Fetch the complete content index at: https://www.moeox.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Wordpress nginx 静态web服务器 缓存 cache
- URL: https://www.moeox.com/wordpress-nginx--e9-9d-99-e6-80-81web-e6-9c-8d-e5-8a-a1-e5-99-a8--e7-bc-93-e5-ad-98-cache/
- Published: 2024-06-24T10:01:29.000Z
- Updated: 2024-06-24T10:01:29.000Z
- Author: asumi233
- Tags: Linux, 杂项, 运维, #wp, #wp-post, #Import 2026-09-01 23:51

## Wordpress nginx 静态web服务器 缓存 cache

1.首先wordpress安装nginx helper 插件，帮助你在修改文章添加文章 这些情况下 自动刷新对应页面缓存的作用

2.**WEB服务器准备:**

首先给nginx添加nginx\_cache\_purge缓存清除模块

模块项目地址：官方一直没更新的模块版本地址为[https://github.com/FRiCKLE/ngx\_cache\_purge](https://github.com/FRiCKLE/ngx%5Fcache%5Fpurge?ref=moeox.com)

官方偷偷更新的新版本清除模块，在debian 官方模块里 哈哈，如果使用debian官方自带的nginx，那你可以使用apt来安装 **libnginx-mod-http-cache-purge** 这个模块

如果你想使用自己编译的nginx并使用这个最新的模块 项目地址为：[https://salsa.debian.org/nginx-team/libnginx-mod-http-cache-purge](https://salsa.debian.org/nginx-team/libnginx-mod-http-cache-purge?ref=moeox.com) 切换分支为upstream 这个就是最新版本 2.5.3

编译方法为：

```bash
nginx -V    ###查看编译参数
在编译参数最后 添加 
--add-module=/xxx/libnginx-mod-http-cache-purge/  ### xxx替换为模块文件夹的绝对位置
编译完成后 直接复制nginx编译文件夹下面的objs/nginx 替换 重启服务就行了
```

接下来开始设置nginx 的配置：

首先在http块里添加如下参数：

```bash
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
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;
```

第一行参数里的WORDPRESS可以修改为任意名称，这个是缓存识别的名称

接下来在对应域名server块配置里添加如下参数：

```bash
# POST requests and urls with a query string should always go to PHP
set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $skip_cache 1;
}
if ($query_string != "") {
    set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
    set $skip_cache 1;
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}
location ~ /purge(/.*) {
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
```

**location purge**这个里面的WORDPRESS记得修改为你对应的缓存标识

接下来还需要在 location php 这个识别php文件的里面添加如下参数：

```bash
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid  60m;
```

保存配置 重启nginx 缓存已经配置完成 然后开启nginx helper 可以自动清除缓存