加速国内 WordPress 站点官方后台服务访问(以腾讯轻量为例)

2021-05-21 13:30:00 +08:00
 LuminousKK

通过反代官方 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 解析哦,不然会有些莫名其妙的错误,效果如图:

2373 次点击
所在节点    云计算
7 条回复
CallmeDredd
2021-05-21 13:32:04 +08:00
国内用 WordPress 的好像很少
yimity
2021-05-21 13:58:05 +08:00
既然已经有了轻量云了,为啥不把 WordPress 装到轻量云上?
NEVERCODE
2021-05-21 15:02:08 +08:00
@yimity 哈哈哈哈哈正解
AkideLiu
2021-05-21 16:43:04 +08:00
可能是针对极端用途
naruco
2021-05-21 16:51:17 +08:00
@AkideLiu 是指连接到 wordpress 官方后台服务,看地址:api.wordpress.org
后台的 rss 官方源订阅就是这种;
Showfom
2021-05-22 15:44:25 +08:00
@CallmeDredd 比你想象中要多了去了
Showfom
2021-07-20 12:18:42 +08:00
自建的话,也可以直接用 wp-config.php 的 WP_HTTP_Proxy 参数,这个最简单

https://developer.wordpress.org/reference/classes/wp_http_proxy/

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/778349

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX