通过廉价虚拟主机实现方向代理

有时候公司做营销推广,新购了一批域名,但是未在阿里云备案的域名没法解析到阿里云主机,这时候我们可以使用廉价的香港虚拟主机通过PHP代理的方式使域名可用。

选择合适的php空间,某宝搜索php空间一大堆

购买之后进入控制面板

然后把域名解析到这台虚拟主机,一般是通过CNAME的方式

然后在在控制面板中绑定改域名

然后开启伪静态功能,该功能主要是重新url地址,比如:

原本要访问<code class="EnlighterJSRAW" data-enlighter-language="null">http://aa.x.com/index.php/api/user/add

重写之后只需这样访问<code class="EnlighterJSRAW" data-enlighter-language="null">http://aa.x.com/api/user/add

地址变得更简洁、合理,不开启该功能无法实现反向代理

最后通过ftp文件管理在网站根目录创建两个文件:

  • index.php
<?php
// Turn off all error reporting;
error_reporting(0);
$domain = $_SERVER['SERVER_NAME'];
$mirror = "要代理的域名";
$req = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . " HTTP/1.0\r\n";
$length = 0;
foreach ($_SERVER as $k => $v) {
    if (substr($k, 0, 5) == "HTTP_") {
        $k = str_replace('_', ' ', substr($k, 5));
        $k = str_replace(' ', '-', ucwords(strtolower($k)));
        if ($k == "Host") {
            $v = $mirror;
        }
        // Alter "Host" header to mirrored server
        if ($k == "Accept-Encoding") {
            $v = "identity;q=1.0, *;q=0";
        }
        // Alter "Accept-Encoding" header to accept unencoded content only
        if ($k == "Keep-Alive") {
            continue;
        }
        // Drop "Keep-Alive" header
        if ($k == "Connection" && $v == "keep-alive") {
            $v = "close";
        }
        // Alter value of "Connection" header from "keep-alive" to "close"
        $req .= $k . ": " . $v . "\r\n";
    }
}
$body = '';
if (!empty($_SERVER['CONTENT_TYPE'])) {
    $body = @file_get_contents('php://input');
    $req .= "Content-Type: " . $_SERVER['CONTENT_TYPE'] . "\r\n";
    $req .= "Content-Length: " . strlen($body) . "\r\n";
}
$req .= "\r\n";
$req .= $body;
// print
$req;
$fp = fsockopen($mirror, 80, $errno, $errmsg, 30);
if (!$fp) {
    print "HTTP/1.0 502 Failed to connect remote server\r\n";
    print "Content-Type: text/html\r\n\r\n";
    print "Failed to connect to {$mirror} due to:<br>[{$errno}] {$errstr}";
    exit;
}
fwrite($fp, $req);
$headers_processed = 0;
$response = '';
while (!feof($fp)) {
    $r = fread($fp, 8192);
    if (!$headers_processed) {
        $response .= $r;
        $nlnl = strpos($response, "\r\n\r\n");
        $add = 4;
        if (!$nlnl) {
            $nlnl = strpos($response, "\n\n");
            $add = 2;
        }
        if (!$nlnl) {
            continue;
        }
        $headers = substr($response, 0, $nlnl);
        $cookies = 'Set-Cookie: ';
        if (preg_match_all('/^(.*?)(\\r?\\n|$)/ims', $headers, $matches)) {
            for ($i = 0; $i < count($matches[0]); ++$i) {
                $ct = $matches[1][$i];
                // if (substr($ct, 0, 12) == "Set-Cookie: ") {
                // $cookies .= substr($ct, 12) . ',';
                // header($cookies);
                // } else header($ct, false);
                // print '>>' . $ct . "\r\n";
            }
        }
        print substr($response, $nlnl + $add);
        $headers_processed = 1;
    } else {
        print $r;
    }
}
fclose($fp);
?>
  • .htaccess

在win下不能直接创建这样的文件,建议用cmd 的ren命令修改

然后在这个文件内贴入以下内容

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

现在使用你的域名访问吧!

大功告成

评论