正向代理:客戶端主動配置,透過代理服務器訪問目標站點,隱藏客戶端真實 IP。
反向代理:對客戶端透明,客戶端以為直接訪問目標站點,實際請求被代理服務器轉發至後端真實服務器並返回內容。惡意反向代理即攻擊者架設代理服務器「盜取」你的網站內容,對用戶與搜尋引擎呈現為一個完整鏡像站點。
<script type="text/javascript">
if (document.domain !== 'example.com' && document.domain !== 'www.example.com') {
window.location.href = 'https://www.example.com/';
}
</script>
同時防 iframe 嵌入:
<script type="text/javascript">
if (top.location !== self.location) top.location = self.location;
</script>
<?php
function proxy_filter() {
return $_SERVER['SERVER_NAME'];
}
$allowed_domains = ['example.com', 'www.example.com'];
$current_domain = proxy_filter();
if (!in_array($current_domain, $allowed_domains)) {
header('HTTP/1.1 403 Forbidden');
exit('非法反向代理訪問');
// 或重定向:header('Location: https://www.example.com/');
}
?>
.htaccess
RewriteEngine On
RewriteBase /
php_value auto_append_file proxy.php
proxy.php
<?php
$forwarded = getenv("HTTP_X_FORWARDED_FOR");
$host = getenv("HTTP_HOST");
$allowed = ['example.com', 'www.example.com'];
if ($forwarded !== "" && !in_array($host, $allowed)) {
exit('本服務器禁止惡意反向代理!');
}
?>
X-Frame-Options: SAMEORIGIN 或 Content-Security-Policy: frame-ancestors 'self' 防 iframe 嵌入多層防護結合,可有效遏制惡意反向代理,保護網站資源與 SEO 權重。