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

/ 0评 / 5

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

文章来源萌洛测试站,转载请注明出处(/≧▽≦)/

发表评论

电子邮件地址不会被公开。 必填项已用*标注