| <!DOCTYPE html> |
| <html lang="zh-CN"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1" /> |
|
|
| |
| <title>如何用PHP制作一个简单的反向代理 | Mayx的博客</title> |
| <meta name="generator" content="Jekyll v3.9.5" /> |
| <meta property="og:title" content="如何用PHP制作一个简单的反向代理" /> |
| <meta name="author" content="mayx" /> |
| <meta property="og:locale" content="zh_CN" /> |
| <meta name="description" content="解决问题还是很简单的。" /> |
| <meta property="og:description" content="解决问题还是很简单的。" /> |
| <meta property="og:site_name" content="Mayx的博客" /> |
| <meta property="og:type" content="article" /> |
| <meta property="article:published_time" content="2021-01-06T00:00:00+08:00" /> |
| <meta name="twitter:card" content="summary" /> |
| <meta property="twitter:title" content="如何用PHP制作一个简单的反向代理" /> |
| <meta name="google-site-verification" content="huTYdEesm8NaFymixMNqflyCp6Jfvd615j5Wq1i2PHc" /> |
| <meta name="msvalidate.01" content="0ADFCE64B3557DC4DC5F2DC224C5FDDD" /> |
| <meta name="yandex-verification" content="fc0e535abed800be" /> |
| <script type="application/ld+json"> |
| {"@context":"https://schema.org","@type":"BlogPosting","author":{"@type":"Person","name":"mayx"},"dateModified":"2021-01-06T00:00:00+08:00","datePublished":"2021-01-06T00:00:00+08:00","description":"解决问题还是很简单的。","headline":"如何用PHP制作一个简单的反向代理","mainEntityOfPage":{"@type":"WebPage","@id":"/2021/01/06/proxy.html"},"publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"https://avatars0.githubusercontent.com/u/17966333"},"name":"mayx"},"url":"/2021/01/06/proxy.html"}</script> |
| |
|
|
| |
| <link rel="canonical" href="https://mabbs.github.io/2021/01/06/proxy.html" /> |
| <link type="application/atom+xml" rel="alternate" href="/atom.xml" title="Mayx的博客" /> |
| <link rel="alternate" type="application/rss+xml" title="Mayx的博客(RSS)" href="/rss.xml" /> |
| <link rel="alternate" type="application/json" title="Mayx的博客(JSON Feed)" href="/feed.json" /> |
| <link rel="stylesheet" href="/assets/css/style.css?v=1786013449" /> |
| |
| <link rel="stylesheet" href="/Live2dHistoire/live2d/css/live2d.css" /> |
| <link rel="stylesheet" href="/assets/css/gitalk.css" /> |
| <script src="/assets/js/gitalk.min.js"></script> |
| |
| <link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Mayx的博客" /> |
| <link rel="webmention" href="https://webmention.io/mabbs.github.io/webmention" /> |
| <link rel="pingback" href="https://webmention.io/mabbs.github.io/xmlrpc" /> |
| <link rel="preconnect" href="https://summary.mayx.eu.org" crossorigin="anonymous" /> |
| <link rel="prefetch" href="https://www.blogsclub.org/badge/mabbs.github.io" as="image" /> |
| <link rel="blogroll" type="text/xml" href="/blogroll.opml" /> |
| <link rel="me" href="https://github.com/Mabbs" /> |
| <link type="text/plain" rel="author" href="/humans.txt" /> |
| <script src="/assets/js/jquery.min.js"></script> |
| |
| |
| |
| |
| |
| <script> |
| var lastUpdated = new Date("Thu, 06 Aug 2026 18:50:49 +0800"); |
| var BlogAPI = "https://summary.mayx.eu.org"; |
| var GitalkConfig = { |
| clientID: '36557aec4c3cb04f7ac6', |
| clientSecret: 'ac32993299751cb5a9ba81cf2b171cca65879cdb', |
| repo: 'mabbs.github.io', |
| owner: 'Mabbs', |
| admin: ['Mabbs'], |
| distractionFreeMode: false, |
| proxy: 'https://cors-anywhere.mayx.eu.org/?https://github.com/login/oauth/access_token' |
| }; |
| </script> |
| <script src="/assets/js/main.js"></script> |
| |
| |
| |
| <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-137710294-1"></script> |
| <script> |
| window.dataLayer = window.dataLayer || []; |
| function gtag(){dataLayer.push(arguments);} |
| gtag('js', new Date()); |
| gtag('config', 'UA-137710294-1'); |
| </script> |
| |
| <script src="/assets/js/instant.page.js" type="module"></script> |
| |
| </head> |
|
|
| <body> |
| <noscript><marquee style="top: -15px; position: relative;"><small>发现当前浏览器没有启用JavaScript,这不影响你的浏览,但可能会有一些功能无法使用……</small></marquee></noscript> |
| |
| <div class="wrapper"> |
| <header class="h-card"> |
| <h1><a class="u-url u-uid p-name" rel="me" href="/">Mayx的博客</a></h1> |
|
|
| |
| <img src="https://avatars0.githubusercontent.com/u/17966333" fetchpriority="high" class="u-photo" alt="Logo" style="width: 90%; max-width: 300px; max-height: 300px; border-radius: 25%;" /> |
| |
|
|
| <p class="p-note">Mayx's Home Page</p> |
|
|
| <form id="search-input-all" action="/search.html"> |
| <input type="text" name="keyword" placeholder="Search blog posts.." /> <input type="submit" value="搜索" /> |
| </form> |
| <br /> |
|
|
| |
|
|
| |
|
|
| <p class="view"><a class="u-url" href="/Mabbs/">About me</a></p> |
|
|
| <ul class="downloads"> |
| |
| <li style="width: 270px; border-right: none;"><a href="/MayxBlog.tgz">Download <strong>TGZ File</strong></a></li> |
| |
| </ul> |
| </header> |
| <section id="pjax-container" class="h-entry"> |
|
|
| <small><time class="date dt-published" datetime="2021-01-06T00:00:00+08:00">6 January 2021</time> - 字数统计:1509 - 阅读大约需要5分钟 - Hits: <span id="/2021/01/06/proxy.html" class="visitors">Loading...</span></small> |
| <h1 class="p-name">如何用PHP制作一个简单的反向代理</h1> |
|
|
| <p class="view">by <a class="p-author h-card" href="//github.com/Mabbs">mayx</a></p> |
| <div id="outdate" style="display:none;"> |
| <hr /><p> |
| 这是一篇创建于 <span id="outime"></span> 天前的文章,其中的信息可能已经有所发展或是发生改变。 |
| </p> |
| </div> |
| <script> |
| daysold = Math.floor((new Date().getTime() - new Date("Wed, 06 Jan 2021 00:00:00 +0800").getTime()) / (24 * 60 * 60 * 1000)); |
| if (daysold > 90) { |
| document.getElementById("outdate").style.display = "block"; |
| document.getElementById("outime").innerHTML = daysold; |
| } |
| </script> |
|
|
| <hr /> |
|
|
| <b>AI摘要</b> |
| <p id="ai-output">这篇文章讲述了作者在被国内运营商屏蔽Github Pages后,如何用PHP制作一个简单的反向代理以解决博客访问问题的经历。作者先尝试使用PHP的file_get_contents来实现反代,但遇到了CSS和JS MIME类型问题。通过研究Header,作者重写了请求的Header信息,最终实现通过PHP脚本重定向到目标URL,并解决了MIME类型的匹配问题。文章最后,作者成功地将博客域名更换并调整了Gitalk的返回地址,让博客在中国用户中正常工作,同时表达了对网络环境改善的希望。</p> |
|
|
| <hr /> |
|
|
|
|
|
|
| <ul><li><a href="#起因">起因</a></li><li><a href="#解决过程">解决过程</a></li><li><a href="#之后的操作">之后的操作</a></li></ul> |
| <hr /> |
|
|
|
|
| <main class="post-content e-content" role="main"><p>解决问题还是很简单的。</p> |
| <h1 id="起因"> |
| |
| |
| <a href="#起因"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 起因 |
| |
| |
| </h1> |
| |
| <p>由于莫名其妙的原因,很多运营商都屏蔽了Github Pages,导致我上我的博客很困难。这令我很不高兴,但是没办法,毕竟在中国就是这样,搭个网站很麻烦。 </p><p> |
| 在两年前,我的博客也被屏蔽过一次,那时候我是用的Nginx反代解决这个问题的,不过Nginx反代要服务器,当时我用的是花火学园的服务器来搞,但是现在因为登一次花火学园的服务器很麻烦,网络卡的不得了,所以没办法,我只好想想别的更简单的方法解决这个问题。 </p><p> |
| 这时候我就想到了PHP,用PHP解决这个问题应该很简单,用<code class="language-plaintext highlighter-rouge">file_get_contents()</code>应该很轻松就可以解决这个问题。</p> |
| <h1 id="解决过程"> |
| |
| |
| <a href="#解决过程"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 解决过程 |
| |
| |
| </h1> |
| |
| <p>理论上来说,只要整一个php然后里面直接用<code class="language-plaintext highlighter-rouge">echo file_get_contents(url)</code>就可以实现反代了,所以首先就按这个思路做,为了让访问的所有请求都发送到这个php里,我得用重写的方式。 </p><p> |
| 我用的是GearHost的虚拟主机,他们的主机都用的是IIS,这导致我还得了解一下IIS的重写怎么搞,还好我的方案和WordPress的重写方案很像,我就直接按照WordPress的方式写了。 </p><p> |
| 把下面的代码放到Rewrite的rules段里就可以正常工作了:</p> |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">name=</span><span class="s">"Mayx"</span> <span class="na">stopProcessing=</span><span class="s">"true"</span><span class="nt">></span> |
| <span class="nt"><match</span> <span class="na">url=</span><span class="s">"^(.*)$"</span> <span class="nt">/></span> |
| <span class="nt"><conditions></span> |
| <span class="nt"><add</span> <span class="na">input=</span><span class="s">"{REQUEST_FILENAME}"</span> <span class="na">matchType=</span><span class="s">"IsFile"</span> <span class="na">negate=</span><span class="s">"true"</span> <span class="nt">/></span> |
| <span class="nt"><add</span> <span class="na">input=</span><span class="s">"{REQUEST_FILENAME}"</span> <span class="na">matchType=</span><span class="s">"IsDirectory"</span> <span class="na">negate=</span><span class="s">"true"</span> <span class="nt">/></span> |
| <span class="nt"></conditions></span> |
| <span class="nt"><action</span> <span class="na">type=</span><span class="s">"Rewrite"</span> <span class="na">url=</span><span class="s">"index.php"</span><span class="nt">/></span> |
| <span class="nt"></rule></span> |
| </code></pre></div></div> |
| <p>然后就是新建一个index.php文件,路径的话用<code class="language-plaintext highlighter-rouge">$_SERVER['REQUEST_URI']</code>就可以了。 </p><p> |
| 不过如果直接用<code class="language-plaintext highlighter-rouge">echo file_get_contents(url)</code>的方法搞会出现一些问题,比如css和js的MIME类型要求必须和实际一样,但是我这个方案会导致所有请求都是<code class="language-plaintext highlighter-rouge">text/html</code>类型的,这是个很麻烦的问题啊…… </p><p> |
| 后来我花了2个小时查资料,搜php怎么读MIME,结果搜到的全是<code class="language-plaintext highlighter-rouge">mime_content_type()</code>之类乱七八糟的东西,让人很难受。我仔细思考了一下,MIME是在Header里声明的,我应该去搜Header,而不是怎么读MIME,最终可算是搜到了一点有用的东西了,也就是<code class="language-plaintext highlighter-rouge">$http_response_header</code>,这个东西好像是个数组,后来我想了一下干脆不要考虑怎么读MIME,直接把Header跟着重写一遍算了,于是最终代码如下:</p> |
| <div class="language-php highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp"><?php</span> |
| <span class="nv">$content</span><span class="o">=</span><span class="nb">file_get_contents</span><span class="p">(</span><span class="s2">"https://mabbs.github.io"</span><span class="mf">.</span><span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">'REQUEST_URI'</span><span class="p">]);</span> |
| <span class="k">foreach</span><span class="p">(</span><span class="nv">$http_response_header</span> <span class="k">as</span> <span class="nv">$header</span><span class="p">){</span> |
| <span class="nb">header</span><span class="p">(</span><span class="nv">$header</span><span class="p">);</span> |
| <span class="p">}</span> |
| <span class="k">if</span><span class="p">(</span><span class="nv">$content</span><span class="p">){</span> |
| <span class="k">echo</span> <span class="nv">$content</span><span class="p">;</span> |
| <span class="p">}</span><span class="k">else</span><span class="p">{</span> |
| <span class="k">echo</span> <span class="s2">"404"</span><span class="p">;</span> |
| <span class="p">}</span> |
| </code></pre></div></div> |
| <h1 id="之后的操作"> |
| |
| |
| <a href="#之后的操作"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 之后的操作 |
| |
| |
| </h1> |
| |
| <p>这么搞完之后基本上就可以正常工作了,于是我的博客域名再一次更换(专供中国用户):<a href="https://yuki.gear.host/">yuki.gear.host</a>,另外把备用的Gitalk的Github Apps返回地址也改了,基本上就可以正常工作了。希望以后GFW能做个人,不要再乱搞国内的网络环境了。</p></main> |
|
|
|
|
| <small style="display: block">tags: <a rel="category tag" class="p-category" href="/search.html?keyword=PHP"><em>PHP</em></a> - <a rel="category tag" class="p-category" href="/search.html?keyword=%E4%BB%A3%E7%90%86"><em>代理</em></a> <span style="float: right;"><a href="https://gitlab.com/mayx/mayx.gitlab.io/tree/master/_posts/2021-01-06-proxy.md">查看原始文件</a></span></small> |
|
|
|
|
| <h4 style="border-bottom: 1px solid #e5e5e5;margin: 2em 0 5px;">推荐文章</h4> |
| <p id="suggest-container">Loading...</p> |
| <script> |
| var suggest = $("#suggest-container"); |
| $.get(BlogAPI + "/suggest?id=/2021/01/06/proxy.html&update=" + lastUpdated.valueOf(), function (data) { |
| if (data.length) { |
| getSearchJSON(function (search) { |
| suggest.empty(); |
| var searchMap = {}; |
| for (var i = 0; i < search.length; i++) { |
| searchMap[search[i].url] = search[i]; |
| } |
| |
| var tooltip = $('<div class="content-tooltip"></div>').appendTo('body').hide(); |
| for (var j = 0; j < data.length; j++) { |
| var item = searchMap[data[j].id]; |
| if (item) { |
| var link = $('<a href="' + item.url + '">' + item.title + '</a>'); |
| var contentPreview = item.content.substring(0, 100); |
| if (item.content.length > 100) { |
| contentPreview += "……"; |
| } |
| link.hover( |
| function(e) { |
| tooltip.text($(this).data('content')) |
| .css({ |
| top: e.pageY + 10, |
| left: e.pageX + 10 |
| }) |
| .show(); |
| }, |
| function() { |
| tooltip.hide(); |
| } |
| ).mousemove(function(e) { |
| tooltip.css({ |
| top: e.pageY + 10, |
| left: e.pageX + 10 |
| }); |
| }).data('content', contentPreview); |
| |
| suggest.append(link); |
| suggest.append(' - ' + item.date + '<br />'); |
| } |
| } |
| }); |
| } else { |
| suggest.html("暂无推荐文章……"); |
| } |
| }); |
| </script> |
|
|
| <br /> |
| <div class="pagination"> |
| |
| <span class="prev"> |
| <a href="/2021/01/04/summary.html"> |
| 上一篇:年终总结 |
| </a> |
| </span> |
| |
| <br /> |
| |
| <span class="next"> |
| <a href="/2021/01/17/picore.html"> |
| 下一篇:piCore的使用经历 |
| </a> |
| </span> |
| |
| </div> |
|
|
| |
|
|
| <div id="gitalk-container" data-page-id="/2021/01/06/proxy"></div> |
|
|
| <script> |
| var gitalk = new Gitalk($.extend({ id: '/2021/01/06/proxy' }, GitalkConfig)); |
| gitalk.render('gitalk-container'); |
| </script> |
| |
|
|
| </section> |
| |
| <div id="landlord" style="left:5px;bottom:0px;"> |
| <div class="message" style="opacity:0"></div> |
| <canvas id="live2d" width="500" height="560" class="live2d"></canvas> |
| <div class="live_talk_input_body"> |
| <form id="live_talk_input_form"> |
| <div class="live_talk_input_name_body" > |
| <input type="checkbox" id="load_this" /> |
| <input type="hidden" id="post_id" value="/2021/01/06/proxy.html" /> |
| <label for="load_this"> |
| <span style="font-size: 11px; color: #fff;"> 想问这篇文章</span> |
| </label> |
| </div> |
| <div class="live_talk_input_text_body"> |
| <input name="talk" type="text" class="live_talk_talk white_input" id="AIuserText" autocomplete="off" placeholder="要和我聊什么呀?" /> |
| <button type="submit" class="live_talk_send_btn" id="talk_send">发送</button> |
| </div> |
| </form> |
| </div> |
| <input name="live_talk" id="live_talk" value="1" type="hidden" /> |
| <div class="live_ico_box" style="display:none;"> |
| <div class="live_ico_item type_info" id="showInfoBtn"></div> |
| <div class="live_ico_item type_talk" id="showTalkBtn"></div> |
| <div class="live_ico_item type_music" id="musicButton"></div> |
| <div class="live_ico_item type_youdu" id="youduButton"></div> |
| <div class="live_ico_item type_quit" id="hideButton"></div> |
| <input name="live_statu_val" id="live_statu_val" value="0" type="hidden" /> |
| <audio src="" style="display:none;" id="live2d_bgm" data-bgm="0" preload="none"></audio> |
| <input id="duType" value="douqilai" type="hidden" /> |
| </div> |
| </div> |
| <div id="open_live2d">召唤伊斯特瓦尔</div> |
| |
| <footer> |
| <p> |
| <small>Made with ❤ by Mayx<br />Last updated at 2026-08-06 18:50:49<br /> 总字数:645545 - 文章数:185 - <a href="/rss.xml">Feed</a> - <a href="/README.html" >About</a></small> |
| </p> |
| </footer> |
| </div> |
| <script src="/assets/js/scale.fix.js"></script> |
| |
| <script src="/assets/js/main_new.js"></script> |
| <script src="/assets/js/blog-console.js"></script> |
| <script src="/Live2dHistoire/live2d/js/live2d.js"></script> |
| <script src="/Live2dHistoire/live2d/js/message.js"></script> |
| <script src="/assets/js/jquery.pjax.min.js"></script> |
| <script src="/assets/js/pjax.js"></script> |
| |
| </body> |
| </html> |
|
|