前言简介
Nginx与Apache都是非常优秀的WEB服务器,不过近年来 Nginx 越来越火,而 Apache 显得有些老态,特别是在高访问量的场景下, Nginx 的处理效率确实不俗。
如果出于某种原因仍然喜欢Apache,并且想加快WordPress网站的速度,则可以在Apache前面放置一个Nginx反向代理缓存解决方案。
本文介绍的方法中,Nginx反向代理缓存会在Apache前面运行,nginx侦听端口80,而Apache侦听端口8080,nginx将负责缓存它可以缓存的所有,同时将其他请求发送到Apache,由Apache处理PHP、MySQL或MariaDB。
注意:不教程不适应于WooCommerce。后续可能会发布适用于WooCommerce的Nginx代理缓存教程。如果您有什么好的建议,欢迎与我们联系。
Apache配置步骤
打开Apache端口文件:
sudo nano /etc/apache2/ports.conf
Listen 8080
sudo nano /etc/apache2/sites-available/wordpress.conf
<VirtualHost *:8080>
安装Nginx
sudo apt-get install nginx nginx-extras -y
配置nginx:
sudo nano /etc/nginx/sites-available/reverse
upstream sent too big header while reading response header from upstream errors with buffers
# WP zhanzhangb nginx proxy cache # Author Mike from https://www.zhanzhangb.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-Zhanzhangb-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; } }
unlink /etc/nginx/sites-enabled/default
sudo service apache2 restart sudo service nginx restart
sudo apt-get install curl -y
curl -I https://www.zhanzhangb.com/
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-Zhanzhangb-Proxy-Cache: HIT
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-Zhanzhangb-Proxy-Cache: MISS
如果在proxy_cache_path
文件夹中查找,则会看到一堆看似随机的字母和数字,这些字母和数字由levels=1:2
决定。这看起来令人困惑,因为nginx将缓存存储为基于的URL的md5哈希值proxy_cache_key
。我们在上面的配置中使用了$scheme$host$uri
。
$scheme
=http$host
=domain$request_uri
=URL
例如 https://www.zhanzhangb.com/contact-us 这个页面:
$scheme
是 https$host
是 www.zhanzhangb.com$request_uri
是 /contact-us
我们可以通过md5生成器来显示它:
echo https://www.zhanzhangb.com/contact-us | md5sum
5e23a4727f38b99583b20a8381670e0b
nginx基于proxy_cache_path levels=1:2
来创建文件目录结构 。
这里的 levels=1:2,1(代表 7
)成为顶级目录, 2(代表b1
)成为其子目录, md5哈希作为文件名,所以https://www.zhanzhangb.com/contact-us这个页面存储的路径为:
/var/run/proxy-cache/7/b1/5e23a4727f38b99583b20a8381670e0b
清除Nginx反向代理缓存
rm -R /var/run/proxy-cache/*