WordPress 反代搭建 — 宝塔 + Nginx 篇

前几个月我撰写了CloudFlare Workers 搭建 WordPress 反代,其优势在于免费,但速度和稳定性达不到高标准,繁琐的操作步骤更是让很多小白无从下手。

于是就有了今天这篇文章。

材料准备:

  • 1个二级域名
  • 1台国外VPS(首推美国,其次香港)

步骤一:安装宝塔面板与Nginx

这一步不再过多介绍,宝塔官网:https://www.bt.cn,Nginx 在宝塔后台安装即可。

步骤二:建立反代站点并设置HTTPS

这一步也不再过多介绍,直接上图片。

步骤三:设置反代站点伪静态

直接复制以下内容粘贴到伪静态里保存即可。

##Proxy-Start
#api.wordpress.org
location /wp/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://$server_name/wp/developer;
    gzip off;
    sub_filter \/\/api.wordpress.org \/\/$server_name\/wp\/api;
    sub_filter \/\/downloads.wordpress.org \/\/$server_name\/wp\/downloads;
    sub_filter \/\/developer.wordpress.org \/\/$server_name\/wp\/developer;
    sub_filter \/\/wordpress.org \/\/$server_name\/wp\/wordpress;
    sub_filter \/\/wp-themes.com \/\/$server_name\/wp\/themes;
    sub_filter \/\/profiles.wordpress.org \/\/$server_name\/wp\/profiles;
    sub_filter \/\/secure.gravatar.com \/\/$server_name\/gravatar;
    sub_filter \/\/ps.w.org \/\/$server_name\/wp\/ps;
    sub_filter \/\/ts.w.org \/\/$server_name\/wp\/ts;
    sub_filter \/\/s.w.org \/\/$server_name\/wp\/s;
    sub_filter_last_modified on;
    sub_filter_once off;
    sub_filter_types application/json;
}
#downloads.wordpress.org
location /wp/downloads/
{
    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://$server_name/wp/wordpress/download;
    proxy_set_header Accept-Encoding "";
    gzip off;
    sub_filter //api.wordpress.org //$server_name/wp/api;
    sub_filter //downloads.wordpress.org //$server_name/wp/downloads;
    sub_filter //developer.wordpress.org //$server_name/wp/developer;
    sub_filter //wordpress.org //$server_name/wp/wordpress;
    sub_filter //wp-themes.com //$server_name/wp/themes;
    sub_filter //profiles.wordpress.org //$server_name/wp/profiles;
    sub_filter //secure.gravatar.com //$server_name/gravatar;
    sub_filter //ps.w.org //$server_name/wp/ps;
    sub_filter //ts.w.org //$server_name/wp/ts;
    sub_filter //s.w.org //$server_name/wp/s;
    sub_filter_last_modified on;
    sub_filter_once off;
}
#developer.wordpress.org
location /wp/developer/
{
    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 //api.wordpress.org //$server_name/wp/api;
    sub_filter //downloads.wordpress.org //$server_name/wp/downloads;
    sub_filter //developer.wordpress.org //$server_name/wp/developer;
    sub_filter //wordpress.org //$server_name/wp/wordpress;
    sub_filter //wp-themes.com //$server_name/wp/themes;
    sub_filter //profiles.wordpress.org //$server_name/wp/profiles;
    sub_filter //secure.gravatar.com //$server_name/gravatar;
    sub_filter //ps.w.org //$server_name/wp/ps;
    sub_filter //ts.w.org //$server_name/wp/ts;
    sub_filter //s.w.org //$server_name/wp/s;
    sub_filter_last_modified on;
    sub_filter_once off;
}
#wordpress.org
location /wp/wordpress/
{
    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 //api.wordpress.org //$server_name/wp/api;
    sub_filter //downloads.wordpress.org //$server_name/wp/downloads;
    sub_filter //developer.wordpress.org //$server_name/wp/developer;
    sub_filter //wordpress.org //$server_name/wp/wordpress;
    sub_filter //wp-themes.com //$server_name/wp/themes;
    sub_filter //profiles.wordpress.org //$server_name/wp/profiles;
    sub_filter //secure.gravatar.com //$server_name/gravatar;
    sub_filter //ps.w.org //$server_name/wp/ps;
    sub_filter //ts.w.org //$server_name/wp/ts;
    sub_filter //s.w.org //$server_name/wp/s;
    sub_filter_last_modified on;
    sub_filter_once off;
}
#wp-themes.com
location /wp/themes/
{
    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 //api.wordpress.org //$server_name/wp/api;
    sub_filter //downloads.wordpress.org //$server_name/wp/downloads;
    sub_filter //developer.wordpress.org //$server_name/wp/developer;
    sub_filter //wordpress.org //$server_name/wp/wordpress;
    sub_filter //wp-themes.com //$server_name/wp/themes;
    sub_filter //profiles.wordpress.org //$server_name/wp/profiles;
    sub_filter //secure.gravatar.com //$server_name/gravatar;
    sub_filter //ps.w.org //$server_name/wp/ps;
    sub_filter //ts.w.org //$server_name/wp/ts;
    sub_filter //s.w.org //$server_name/wp/s;
    sub_filter_last_modified on;
    sub_filter_once off;
}
#profiles.wordpress.org
location /wp/profiles/
{
    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;
}
#ps.w.org
location /wp/ps/
{
    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_set_header Accept-Encoding "";
    gzip off;
    sub_filter //api.wordpress.org //$server_name/wp/api;
    sub_filter //downloads.wordpress.org //$server_name/wp/downloads;
    sub_filter //developer.wordpress.org //$server_name/wp/developer;
    sub_filter //wordpress.org //$server_name/wp/wordpress;
    sub_filter //wp-themes.com //$server_name/wp/themes;
    sub_filter //profiles.wordpress.org //$server_name/wp/profiles;
    sub_filter //secure.gravatar.com //$server_name/gravatar;
    sub_filter //ps.w.org //$server_name/wp/ps;
    sub_filter //ts.w.org //$server_name/wp/ts;
    sub_filter //s.w.org //$server_name/wp/s;
    sub_filter_last_modified on;
    sub_filter_once off;
}
#ts.w.org
location /wp/ts/
{
    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_set_header Accept-Encoding "";
    gzip off;
    sub_filter //api.wordpress.org //$server_name/wp/api;
    sub_filter //downloads.wordpress.org //$server_name/wp/downloads;
    sub_filter //developer.wordpress.org //$server_name/wp/developer;
    sub_filter //wordpress.org //$server_name/wp/wordpress;
    sub_filter //wp-themes.com //$server_name/wp/themes;
    sub_filter //profiles.wordpress.org //$server_name/wp/profiles;
    sub_filter //secure.gravatar.com //$server_name/gravatar;
    sub_filter //ps.w.org //$server_name/wp/ps;
    sub_filter //ts.w.org //$server_name/wp/ts;
    sub_filter //s.w.org //$server_name/wp/s;
    sub_filter_last_modified on;
    sub_filter_once off;
}
#s.w.org
location /wp/s/
{
    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;
}
#secure.gravatar.com
location /gravatar/
{
    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;
}
##End

步骤四:使用

添加以下内容到主题的functions.php中,注意替换反代域名为自己的。

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', '你的反代域名/wp/api', $url);
    $url = str_replace('downloads.wordpress.org', '你的反代域名/wp/downloads', $url);
    return wp_remote_request($url, $r);
}

add_filter('pre_http_request', 'my_pre_http_request', 10, 3);

赞(1) 打赏
转载请保留出处:耗子博客 » WordPress 反代搭建 — 宝塔 + Nginx 篇
分享到: 更多 (0)

评论 2

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

金额随意,一百两百也不介意的啦

支付宝扫一扫打赏

微信扫一扫打赏

Loading