前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >反代加速国内 WordPress 站点官方后台服务访问

反代加速国内 WordPress 站点官方后台服务访问

原创
作者头像
LMSKK
修改2021-05-21 14:35:47
1.1K0
修改2021-05-21 14:35:47
举报
文章被收录于专栏:Luminous的云计算杂谈

通过反代官方 API 加速位于国内的 WordPress 站点访问更新、主题等等服务的方法,最初国内 429 的时候 WP-ChinaYes 那个项目提出来的。那时候研究了下用一个域名完成整个反代,稳定使用了接近一年了,分享出来~

WP-ChinaYes 后来走了劫持并二次处理官方 API 的路线,个人不是很喜欢;而且个人需求也没到他们规划的程度,自己搭建的安全性方面也更放心。

在这里服务器推荐腾讯的轻量,香港或者硅谷到大陆链路都不错,我测试到国内腾讯云、华为云、UCloud 等等云厂都是走的 CN2 回国,24 一个月速度很棒也很划算,拿来搭建这样的服务非常合适。

*注意鹅厂的轻量不能给你在阿里云国内机器上的用,两家不和绕的很感人很慢很慢。


第一步准备一个反代的域名,例如 proxy.example.com (文件中 20 处自行替换),SSL 自己配置好,反代的配置文件如下:

代码语言:javascript
复制
#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 改成你搭建好的反代:

代码语言:javascript
复制
/**
  * 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 解析哦,不然会有些莫名其妙的错误,效果如图:

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档