使用 Fast-CGI cache 优化 Nginx 加速 WordPress 站点

系统为 Debian 10

一、重新编译并安装 nginx

1.卸载原有的 nginx

sudo apt remove nginx nginx-common

注:该命令不会删除 nginx 的站点配置文件。

2.获取依赖

重新编译 nginx 需要 OpenSSLZlibPCRE等库的依赖,谷歌即可获得三个库的下载链接(注意OpenSSL选用LTS版本)

OpenSSLhttps://www.openssl.org/

Zlibhttps://www.zlib.net/

PCREhttps://www.pcre.org/

# PCRE version 8.43
# 2022-6-14 更新:下行PCRE链接已不再可用,需更换至PCRE2
wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz && tar -xzvf pcre-8.45.tar.gz

# zlib version 1.2.11
wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar -xzvf zlib-1.2.11.tar.gz

# OpenSSL version 1.1.1k
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1k.tar.gz && tar -xzvf openssl-1.1.1k.tar.gz

对每个所需的依赖库,可以先单独进入文件夹 configure 一下看看会不会出错,其中PCRE configure 时遇到的一个错误可见踩坑记录 1。

安装可选的 nginx 依赖:

sudo apt install libgd-dev libgeoip-dev libxslt-dev

3.获取要自定义安装的模块

重新编译安装的目的就是为 nginx 加上 ngx_cache_purge这个模块,使用git克隆官方仓库:

git clone https://github.com/FRiCKLE/ngx_cache_purge.git

4.编译、安装 Nginx

下载 nginx 源码包:

wget http://nginx.org/download/nginx-1.20.1.tar.gz && tar -zvxf nginx-1.20.1.tar.gz

进入 nginx 源码文件夹并进行配置、编译与安装:

$ cd nginx-1.20.1
$ ./configure --with-pcre=../pcre-8.45 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.1k --add-module=/path/to/ngx_cache_purge ...
# 完整 configure 文件见附录
$ make
$ sudo make install # 若有 checkinstall 建议使用

创建 nginx 模块的软链接,将其链接至标准位置:

sudo ln -s /usr/lib/nginx/modules /etc/nginx/modules

如果此时使用的附录 self-compiled 所配置的软链接,则 root 执行 nginx -V可以看到输出。

为 nginx 创建 systemd 单元:

sudo vim /etc/systemd/system/nginx.service

nginx.service 中复制粘贴以下内容(参考 Vultr 官网教程1):

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

之后使用 systemd 即可控制 nginx 服务的启动,停止等:

sudo systemctl start nginx
sudo systemctl enable nginx

如果 systemd 提示 Unmasked Unit,执行 sudo systemctl unmask nginx.service,具体原因可能是最初卸载 nginx 时自动使用 mask 将 nginx 服务链接至了/dev/null

至此,编译安装 nginx 的步骤完成,打开网站应当正常工作。

二、配置 Fast CGI Cache with Purge

1.安装 Nginx Helper 插件

自行在 WordPress 插件商店中搜索安装即可,启用插件的 Purge。

2.编辑 Nginx 配置文件

参考2,在 /etc/nginx/nginx.conf文件中的http字段内修改如下:

http{
    ...
    # 粘贴下列四行
    fastcgi_cache_path /var/cache/nginx 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;
    ...
}

/etc/nginx/sites-avaliable/example.com.conf文件中的server字段内粘贴如下内容:

server{
    ...
    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;
    }
    
    ... 
    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        include fastcgi_params;
        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
​
        fastcgi_cache WORDPRESS;
        fastcgi_cache_valid  60m;
        add_header X-FastCGI-Cache $upstream_cache_status;
    }
​
    location ~ /purge(/.*) {
        fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
    }
    ...
}

保存配置文件后,使用nginx -t检查配置文件是否有错,通过后重启 nginx 即可。

至此,Nginx 配置 Fast CGI Cache 完成。作者对 WEB 了解很少,有错欢迎指出。

三、检查效果

打开浏览器访问站点,并开启检查功能,访问一次网页,并查看响应的标头,X-FastCGI-Cache的值应为 HIT,表示缓存命中,如本站的响应如下:

HTTP/1.1 304 Not Modified
Server: nginx/1.20.1
Date: Wed, 20 Oct 2021 02:32:03 GMT
Connection: keep-alive
Cache-Control: no-cache
WPO-Cache-Status: cached
Last-Modified: Tue, 19 Oct 2021 10:02:57 GMT
X-FastCGI-Cache: HIT

或者直接在服务器执行curl -I https://example.com来查看响应。

踩坑记录

1. configure PCRE2 时出错

configure 的提示信息为configure:4047: error: C compiler cannot create executables,打开config.log文件查找具体错误原因。

config.log中可以看见多个error,但有些error只是 configure 程序为了寻找合适 gcc 版本所进行的尝试,真正的错误原因定位在:

/usr/bin/ld: cannot find Scrt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory

Google 了一下错误,可能是缺少多架构标准库所导致的,尝试安装gcc-multilib

sudo apt install gcc-multilib

再次 configure 后成功。

附录

1. nginx configure 配置文件备份

apt installed:

[CENSORED]

self-compiled:

[CENSORED]

参考

[1]  https://www.vultr.com/docs/how-to-compile-nginx-from-source-on-debian-10

[2]  https://easyengine.io/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/

《使用 Fast-CGI cache 优化 Nginx 加速 WordPress 站点》上有1条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据