由于 WordPress 服务器放在家里,而家庭网络不给开 80、443 端口,于是就在 VPS 上部署 Nginx 做反向代理。但 VPS 是国外的机子,再访回国内的网就比较慢了。通过 PageSpeed Insights 测试,也反应出服务器响应最大的问题。开启缓存功能后,评分从原来的 83 分一下子就到了 97 分。

以下是具体配置,完全拷贝自[1],做个备份吧。

#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-Bullet-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;
}
}

  1. How to Configure nginx Reverse Proxy WordPress Cache for Apache
  2. Nginx Reverse Proxy Cache for WordPress
  3. lukeburden / nginx_blog.conf

发表回复

您的电子邮箱地址不会被公开。