加速国内 WordPress 站点官方后台服务访问(以腾讯轻量为例)
通过反代官方 API 加速位于国内的 WordPress 站点访问更新、主题等等服务的方法,*初国内 429 的时候 WP-ChinaYes 那个项目提出来的。那时候研究了下用一个域名完成整个反代,稳定使用了接近一年了,分享出来~
WP-ChinaYes 后来走了劫持并二次处理官方 API 的路线,个人不是很喜欢;而且个人需求也没到他们规划的程度,自己搭建的安全性方面也更放心。
在这里服务器推荐腾讯的轻量,香港或者硅谷到大陆链路都不错,我测试到国内腾讯云、华为云、UCloud 等等云厂都是走的 CN2 回国,24 一个月速度很棒也很划算,拿来搭建这样的服务非常合适。
*注意鹅厂的轻量不能给你在阿里云国内机器上的用,两家不和绕的很感人很慢很慢。
准备一个反代的域名,例如 proxy.example.com (文件中 20 处自行替换),SSL 自己配置好,反代的配置文件如下:
#PROXY-API
location /api/
{
proxy_pass https://api.wordpress.org/;
proxy_set_header Host api.wordpress.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Accept-Encoding “”;
proxy_redirect https://developer.wordpress.org https://proxy.example.com/developer.wordpress.org;
gzip off;
sub_filter ps.w.org proxy.example.com/ps.w.org;
sub_filter downloads.wordpress.org proxy.example.com/dl;
sub_filter profiles.wordpress.org proxy.example.com/profiles.wordpress.org;
sub_filter secure.gravatar.com proxy.example.com/secure.gravatar.com;
sub_filter 0.gravatar.com proxy.example.com/secure.gravatar.com;
sub_filter 1.gravatar.com proxy.example.com/secure.gravatar.com;
sub_filter 2.gravatar.com proxy.example.com/secure.gravatar.com;
sub_filter ts.w.org proxy.example.com/ts.w.org;
sub_filter wp-themes.com proxy.example.com/wp-themes.com;
sub_filter s.w.org proxy.example.com/s.w.org;
sub_filter wordpress.org proxy.example.com/wordpress.org;
sub_filter_last_modified on;
sub_filter_once off;
sub_filter_types application/json;
}
#PROXY-DL
location /dl/
{
proxy_pass https://downloads.wordpress.org/;
proxy_set_header Host downloads.wordpress.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_redirect https://wordpress.org/download/ https://proxy.example.com/wordpress.org/download/;
}
#PROXY-Developer
location /developer.wordpress.org/
{
proxy_pass https://developer.wordpress.org/;
proxy_set_header Host developer.wordpress.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Accept-Encoding “”;
gzip off;
sub_filter downloads.wordpress.org proxy.example.com/dl;
sub_filter developer.wordpress.org proxy.example.com/developer.wordpress.org;
sub_filter ps.w.org proxy.example.com/ps.w.org;
sub_filter_last_modified on;
sub_filter_once off;
}
#PROXY-WPORG
location /wordpress.org/
{
proxy_pass https://wordpress.org/;
proxy_set_header Host wordpress.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Accept-Encoding “”;
gzip off;
sub_filter //wordpress.org //proxy.example.com/wordpress.org;
sub_filter s.w.org proxy.example.com/s.w.org;
sub_filter downloads.wordpress.org proxy.example.com/downloads.wordpress.org;
sub_filter_last_modified on;
sub_filter_once off;
}
#PROXY-WP-Theme
location /wp-themes.com/
{
proxy_pass https://wp-themes.com/;
proxy_set_header Host wp-themes.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Accept-Encoding “”;
gzip off;
sub_filter wp-themes.com proxy.example.com/wp-themes.com;
sub_filter_last_modified on;
sub_filter_once off;
}
#PROXY-Profiles
location /profiles.wordpress.org/
{
proxy_pass https://profiles.wordpress.org/;
proxy_set_header Host profiles.wordpress.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
}
#PROXY-Secure
location /secure.gravatar.com/
{
proxy_pass https://secure.gravatar.com/;
proxy_set_header Host secure.gravatar.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
}
#PROXY-Ps
location /ps.w.org/
{
proxy_pass https://ps.w.org/;
proxy_set_header Host ps.w.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
}
#PROXY-TS
location /ts.w.org/
{
proxy_pass https://ts.w.org/;
proxy_set_header Host ts.w.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
}
#PROXY-S
location /s.w.org/
{
proxy_pass https://s.w.org/;
proxy_set_header Host s.w.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
}
然后把 WP-ChinaYes 的核心代码加到主题的 functions.php 即可,proxy.example.com 改成你搭建好的反代:
/**
* WP-ChinaYes
* https://github.com/litepress/wp-china-yes/tree/custom
*/
function my_pre_http_request($preempt, $r, $url) {
if ( ! stristr($url, ‘api.wordpress.org’) && ! stristr($url, ‘downloads.wordpress.org’)) {
return false;
}
$url = str_replace(‘api.wordpress.org’, ‘proxy.example.com/api’, $url);
$url = str_replace(‘downloads.wordpress.org’, ‘proxy.example.com/dl’, $url);
return wp_remote_request($url, $r);
}
add_filter(‘pre_http_request’, ‘my_pre_http_request’, 10, 3);
*后就是注意,这个 NGINX 的 vhost 一定要关闭掉 php 解析哦,不然会有些莫名其妙的错误,效果如图:
g7oOPS.png
sub_filter proxy_set_header x-real-ip proxy_pass6 条回复 • 2021-05-22 15:44:25 +08:00
CallmeDredd 1
CallmeDredd 21 天前
国内用 WordPress 的好像很少
yimity 2
yimity 21 天前
既然已经有了轻量云了,为啥不把 WordPress 装到轻量云上?
NEVERCODE 3
NEVERCODE 21 天前
@yimity 哈哈哈哈哈正解
AkideLiu 4
AkideLiu 20 天前 via iPhone
可能是针对*端用途
naruco 5
naruco 20 天前
@AkideLiu 是指连接到 wordpress 官方后台服务,看地址:api.wordpress.org
后台的 rss 官方源订阅就是这种;
Showfom 6
Showfom 20 天前 via iPhone
@CallmeDredd 比你想象中要多了去了