Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Linux 禁用 Apache/Nginx 空主機頭:防止惡意域名解析綁定

編輯:關於伺服器

空主機頭(默認虛擬主機)允許任意域名解析至伺服器 IP 直接訪問,易被惡意綁定、內容鏡像、SEO 劫持。必須顯式拒絕或導向錯誤頁。

Apache 禁用空主機頭

# /etc/httpd/conf/httpd.conf
NameVirtualHost *:80

# 第一優先級:捕獲所有未匹配的請求
<VirtualHost *:80>
    ServerName dummy.default
    DocumentRoot /var/www/html_default
    <Directory /var/www/html_default>
        Require all denied
    </Directory>
    # 選項 A:返回 403
    # 選項 B:返回 444 (Nginx 專用,Apache 需模組)
    # 選項 C:重導向至正規域名
    RewriteEngine On
    RewriteRule ^(.*)$ https://www.example.com$1 [R=301,L]
</VirtualHost>

# 正規站點配置(其後匹配)
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com *.example.com
    DocumentRoot /var/www/html
    # 正常站點配置...
</VirtualHost>

Nginx 禁用空主機頭

# /usr/local/nginx/conf/nginx.conf
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;  # 無效主機名,匹配所有未匹配請求
    
    # 方案 1:直接關閉連接(Nginx 專有,最高效)
    return 444;
    
    # 方案 2:返回 403
    # return 403;
    
    # 方案 3:重導向至正規域名
    # rewrite ^(.*)$ https://www.example.com$1 permanent;
}

驗證方法

# 直接用 IP 訪問
curl -H "Host: " https://server-ip/
curl -H "Host: malicious.com" https://server-ip/
# 應返回 444/403/301,而非正常站點內容

進階:導流至蜜罐/日誌分析

server {
    listen 80 default_server;
    server_name _;
    access_log /var/log/nginx/honeypot.log honeypot;
    return 444;
}

log_format honeypot '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent" "$host"';

注意事項

  • Apache 2.4+ 需 Require all denied 而非舊版 Order deny,allow / Deny from all
  • Nginx default_server 僅能出現一次 per port/IP 組合
  • SSL 虛擬主機同理需配置 listen 443 ssl default_server
  • CDN/WAF 回源 Host 頭通常為源站域名,不會觸發空主機頭規則
Copyright © Windows教程網 All Rights Reserved