众所周知的原因,fonts.googleapis.com和fonts.gstatic.com在国内是基本无法访问的情况,因此使用了Google字体样式资源的WP主题国内体验极差,严重影响网站打开速度。
如果你的WordPress服务器也在国外就好办了,通过自己架设一个nginx代理,进行加速。
方法如下:
1. 安装Nginx
Nginx安装需要--with-http_slice_module
支持slice
指令,和substitutions
模块,不支持的请升级之,参考官网文档:substitutions
CentOS7的也可以直接下载RPM包安装 https://repo.conf.top/centos/7/x86_64/
或者自行RPMBUILD构建,SPEC文件参考 https://git.conf.top/public/rpmbuild
2. Nginx配置
Nginx增加vhost配置,配置如下:
# 开启缓存
#proxy_cache_path /data/cache/nginx/proxy_cache/ levels=1:2 keys_zone=proxy:128m inactive=30d max_size=1024m;
map $request_uri $myproxy_uri {
default "/";
"~^/https?://[^/:]+(:[0-9]{1,5})?(?<myproxyuri>/.*)" "$myproxyuri";
}
map $request_uri $myproxy_host {
default "www.google.com";
"~^/https?://(?<myproxyhost>[a-z0-9\.-]+)[/|:]?" "$myproxyhost";
}
map $request_uri $myproxy_port {
default "";
"~^/https?://[^/:]+(?<myproxyport>:[0-9]{1,5})" "$myproxyport";
}
map $request_uri $myproxy_scheme {
default $scheme;
"~^/(?<myproxyscheme>https?)://" "$myproxyscheme";
}
map $myproxy_uri $myproxy_uri_dir {
default "/";
"~(?<myproxyuridir>/.+/)[^/]+$" "$myproxyuridir";
}
map $myproxy_uri $myproxy_uri_file {
default "";
"~(?<myproxyurifile>[^/]*)$" "$myproxyurifile";
}
server {
listen 80;
listen [::]:80;
#listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name cdn.conf.top;
#include ssl_base.conf;
#include ssl_cert_default.conf;
#access_log off;
#error_log /dev/null
set $nr "0";
if ($myproxy_uri = "/") {
set $nr "${nr}0";
}
if ($request_uri ~ '[^/]$') {
set $nr "${nr}0";
}
if ($nr = "000") {
rewrite . $scheme://$host:$server_port/$myproxy_scheme://$myproxy_host$myproxy_port/ redirect;
}
# 通用配置,页面中大部分链接都可以替换,目前已知问题是用js动态生成的不行
subs_filter_types text/css text/xml text/plain text/javascript application/x-javascript application/javascript application/json;
subs_filter (<|\\s)(src|href|action|srcset)\\s*=\\s*('|")(https?://)([^/'">]+)/ $1$2=$3$scheme://$host:$server_port/\$5/ irg;
subs_filter (<|\\s)(src|href|action|srcset)\\s*=\\s*('|")// $1$2=$3$scheme://$host:$server_port/$myproxy_scheme:// irg;
subs_filter (<|\\s)(src|href|action|srcset)\\s*=\\s*('|")/([^/'">]+) $1$2=$3$scheme://$host:$server_port/$myproxy_scheme://$myproxy_host$myproxy_port/\ irg;
subs_filter (<|\\s)(src|href|action|srcset)\\s*=\\s*('|")\.\./([^/]+['">]) $1$2=$3$scheme://$host:$server_port/$myproxy_scheme://$myproxy_host$myproxy_port/\ irg;
subs_filter (<|\\s)(src|href|action|srcset)\\s*=\\s*('|")(\.\./[^/]+/) $1$2=$3$scheme://$host:$server_port/$myproxy_scheme://$myproxy_host$myproxy_port$myproxy_uri_dir$4 irg;
subs_filter (:|\\s)url\\s*\\(\\s*('|"|)(https?://)([^/'"\\)\;]+)/ $1url($2$scheme://$host:$server_port/\$4/ irg;
subs_filter (:|\\s)url\\s*\\(\\s*('|"|)// $1url($2$scheme://$host:$server_port/$myproxy_scheme:// irg;
subs_filter (:|\\s)url\\s*\\(\\s*('|"|)/([^/'"\\)\;]+) $1url($2$scheme://$host:$server_port/$myproxy_scheme://$myproxy_host$myproxy_port/\ irg;
subs_filter (:|\\s)url\\s*\\(\\s*('|"|)\.\./([^/]+['|"|\\)]) $1url($2$scheme://$host:$server_port/$myproxy_scheme://$myproxy_host$myproxy_port/\ irg;
subs_filter (:|\\s)url\\s*\\(\\s*('|"|)(\.\./[^/]+/) $1url($2$scheme://$host:$server_port/$myproxy_scheme://$myproxy_host$myproxy_port$myproxy_uri_dir$3 irg;
# 开启缓存
#proxy_cache proxy;
#proxy_cache_key $myproxy_host$myproxy_port$myproxy_uri$is_args$args$slice_range;
#proxy_cache_valid 200 206 304 30d;
#proxy_cache_valid 301 302 5s;
#proxy_cache_valid 401 403 404 500 503 5s;
more_set_headers "X-Cache: $upstream_cache_status";
more_set_headers "Access-Control-Allow-Origin: *";
more_set_headers "Access-Control-Allow-Methods: GET";
proxy_hide_header Set-Cookie;
proxy_hide_header Alt-Svc;
proxy_hide_header p3p;
proxy_hide_header x-content-type-options;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { access_log off; echo "User-agent: *"; echo "Disallow: /"; }
# 安全限制,禁止内网代理
if ($myproxy_host ~ '^(127|10|172\.(1[6-9]|2[0-9]|3[0-1])|192\.168)\.') {
return 401;
}
if ($myproxy_host ~* '(localhost|localdomain|conf.top)' ) {
return 401;
}
location /http {
# 安全限制
#if ( $request_method !~ GET|HEAD ) {
# return 403;
#}
slice 1m;
add_header X-Proxy-Scheme $myproxy_scheme;
add_header X-Proxy-Host $myproxy_host;
add_header X-Proxy-Port $myproxy_port;
add_header X-Proxy-Uri $myproxy_uri;
add_header X-Proxy-Uri-Dir $myproxy_uri_dir;
add_header X-Proxy-Uri-File $myproxy_uri_file;
proxy_ignore_headers Expires Cache-Control;
proxy_ssl_server_name on;
proxy_set_header Referer $myproxy_scheme://$myproxy_host$myproxy_port$myproxy_uri;
proxy_set_header Host $myproxy_host;
proxy_set_header Range $slice_range;
proxy_set_header Accept-Encoding "";
proxy_pass $myproxy_scheme://$myproxy_host$myproxy_port$myproxy_uri;
proxy_redirect ~^(/.*) $scheme://$host:$server_port/$myproxy_scheme://$myproxy_host$myproxy_port/\;
proxy_redirect ~^(http.+) $scheme://$host:$server_port/\;
}
location / {
#access_log off;
#error_log /dev/null;
return 403;
}
}
例如:https://fonts.googleapis.com/css?family=Roboto
对应代理后:https://cdn.conf.top/https://fonts.googleapis.com/css?family=Roboto