PHP实反向代理-收藏

需求 

  现在有些后辍的域名不支持备案,这个时候需要用免备案主机或空间做个反向代理,这样可实现内容存放在国内主机统一管理

实现

  用 php-dynamic-mirror 可实现,并在头部进行域名转换,可实现多个域名反向代理到不同的站点,一共两个文件简单方便,做个收藏

  index.php

  

<?php
$domain = $_SERVER['SERVER_NAME'];
$mirror = "www.mz.cn";
switch ($domain)
{
case "www.a.cn":
  $mirror = "www.az.cn";
  break;
case "www.b.com.cn":
  $mirror = "www.bz.com";
  break;
default:
  $mirror = "www.mz.cn";
}

$req = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . " HTTP/1.0
";
$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 . "
";
    }
}
$body = @file_get_contents('php://input');
$req .= "Content-Type: " . $_SERVER['CONTENT_TYPE'] . "
";
$req .= "Content-Length: " . strlen($body) . "
";
$req .= "
";
$req .= $body;

#print $req;

$fp = fsockopen($mirror, 80, $errno, $errmsg, 30);
if (!$fp) {
    print "HTTP/1.0 502 Failed to connect remote server
";
    print "Content-Type: text/html

";
    print "<html><body>Failed to connect to $mirror due to:<br>[$errno] $errstr</body></html>";
    exit;
}

fwrite($fp, $req);

$headers_processed = 0;
$reponse = '';
while (!feof($fp)) {
    $r = fread($fp, 8192);
    if (!$headers_processed) {
        $response .= $r;
        $nlnl = strpos($response, "

");
        $add = 4;
        if (!$nlnl) {
            $nlnl = strpos($response, "

");
            $add = 2;
        }
        if (!$nlnl)
            continue;
        $headers = substr($response, 0, $nlnl);
        $cookies = 'Set-Cookie: ';
        if (preg_match_all('/^(.*?)(
?
|$)/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 . "
";
            }
        print substr($response, $nlnl + $add);
        $headers_processed = 1;
    } else
        print $r;
}
fclose ($fp);
?>

.htaccess

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

文件

  文件下载

 

原文地址:https://www.cnblogs.com/hantianwei/p/6287497.html