Nginx与Apache都是非常优秀的WEB服务器,不过近年来 Nginx 越来越火,而 Apache 显得有些老态,特别是在高访问量的场景下, Nginx 的处理效率确实不俗。所以站长帮一直推荐大家首选 Nginx 。
如果出于某种原因仍然喜欢Apache,并且想加快WordPress网站的速度,则可以在Apache前面放置一个Nginx反向代理缓存解决方案。
本文介绍的方法中,Nginx反向代理缓存会在Apache前面运行,nginx侦听端口80,而Apache侦听端口8080,nginx将负责缓存它可以缓存的所有,同时将其他请求发送到Apache,由Apache处理PHP、MySQL或MariaDB,如下图:

注意:不教程不适应于WooCommerce。后续可能会发布适用于WooCommerce的Nginx代理缓存教程。如果您有什么好的建议,欢迎与我们联系。
如何为Nginx反向代理配置Apache
打开Apache端口文件:
sudo nano /etc/apache2/ports.conf
将端口更改为8080:
Listen 8080
打开Apache虚拟主机配置文件:
sudo nano /etc/apache2/sites-available/wordpress.conf
将Virtualhost端口更改为8080:
<VirtualHost *:8080>
Ctrl + X,Y + Enter保存
需要更改所有Apache虚拟主机侦听端口为8080。
在安装和配置nginx之后,Apache将重新启动。
安装Nginx
安装nginx和nginx-extras
软件包获取ngx_cache_purge模块,这将比nginx代理缓存更方便管理。
sudo apt-get install nginx nginx-extras -y
配置nginx:
sudo nano /etc/nginx/sites-available/reverse
粘贴nginx配置,我们需要proxy_buffer
在顶部防止发生此错误:
upstream sent too big header while reading response header from upstream errors with buffers
这是nginx代理缓存配置实例(不支持WooCommerce优化):
记住要更改Web.Server.IP
服务器的IP地址。
# WP laipang nginx proxy cache
# Author Mike from https://www.laipang.com
#fix 504 gateway timeouts, can go in nginx.conf
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
#set the location of the cached files, zone, name, size (1000 MB) and how long to cache for 600 minutes
proxy_cache_path /var/run/proxy_cache levels=1:2 keys_zone=WORDPRESS-PROXY:10m max_size=1000m inactive=600m use_temp_path=off;
proxy_cache_key $scheme$host$request_uri;
#prevent header too large errors
proxy_buffers 256 16k;
proxy_buffer_size 32k;
#httpoxy exploit protection
proxy_set_header Proxy "";
# add forwarded for header
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server {
listen 80 default;
access_log /var/log/nginx/proxy-access.log;
error_log /var/log/nginx/proxy-error.log;
# show cache status and any skip cache reason
add_header WP-laipang-Proxy-Cache $upstream_cache_status;
add_header Cache-BYPASS-Reason $skip_reason;
# define nginx variables
set $skip_cache 0;
set $skip_reason "";
# security for bypass so localhost can empty cache
if ($remote_addr ~ "^(127.0.0.1|Web.Server.IP)$") {
set $bypass $http_secret_header;
}
# skip caching WordPress cookies
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $skip_cache 1;
set $skip_reason Cookie;
}
# Don't cache URIs containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml") {
set $skip_cache 1;
set $skip_reason URI;
}
location / {
proxy_set_header Host $host;
# may need to comment out proxy_redirect if get login redirect loop
proxy_redirect off;
proxy_cache WORDPRESS-PROXY;
proxy_cache_revalidate on;
proxy_ignore_headers Expires Cache-Control;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_cache_bypass $skip_cache;
proxy_no_cache $skip_cache;
proxy_cache_valid 200 301 302 500m;
proxy_cache_valid 404 1m;
#can rename PURGE to whatever you want, should restrict it to backend server requests for security
proxy_cache_purge PURGE from 127.0.0.1 Web.Server.IP;
# pass requests onto your PHP backend
proxy_pass http://127.0.0.1:8080;
}
# allows purging via special URL
location ~ /purge(/.*) {
allow 127.0.0.1;
allow Web.Server.IP;
deny all;
proxy_cache_purge WORDPRESS-PROXY $scheme$host$1;
}
}
Ctrl + X,Y + Enter保存
Symlink用于WordPress虚拟主机的Nginx反向代理缓存,当重新启动Nginx后启用。
取消默认的Nginx虚拟主机:
unlink /etc/nginx/sites-enabled/default
重新启动Apache和Nginx:
sudo service apache2 restart
sudo service nginx restart
测试Nginx反向代理缓存
我们可以使用cURL来测试nginx反向代理是否缓存了WordPress网站。
安装cURL:
sudo apt-get install curl -y
已经安装了cURL,现在可以开始在Apache前面测试nginx反向代理
在Web服务器上使用SSH运行这些cURL命令。现在测试主页是否被反向代理缓存,-I参数可以从反向代理服务器获取响应头。
curl -I https://www.laipng.com/
这里的关键值是WP-laipang-Proxy-Cache
状态,已缓存的结果示例:
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Wed, 30 Mar 2016 17:32:24 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
WP-lipang-Proxy-Cache: HIT
如果未缓存则WP-laipang-Proxy-Cache
为MISS
:
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Wed, 30 Mar 2016 17:35:53 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
WP-laip