PHP實反向 Proxy-收藏

來源:互聯網
上載者:User

標籤:下載   pen   支援   server   tac   管理   轉換   entity   htm   

需求 

  現在有些後輟的網域名稱不支援備案,這個時候需要用免備案主機或空間做個反向 Proxy,這樣可實現內容存放在國內主機統一管理

實現

  用 php-dynamic-mirror 可實現,並在頭部進行網域名稱轉換,可實現多個網域名稱反向 Proxy到不同的網站,一共兩個檔案簡單方便,做個收藏

  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\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 = @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 "<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, "\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

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

  檔案下載

 

PHP實反向 Proxy-收藏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.