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

企業級防火牆部署 Apache 反向代理:多內站統一對外發布全流程

編輯:Linux 教學

架構拓撲

Internet
   │
   ▼
[防火牆/邊緣網關]  ← Apache Reverse Proxy (1.2.3.4:80/443)
   │  eth0: 1.2.3.4 (公網)
   │  eth1: 192.168.2.1 (內網網關)
   ▼
┌─────────┬─────────┬─────────┐
│ Web A   │ Web B   │ Web C   │  內網服務器 (私有 IP)
│192.168.2.2│192.168.2.3│192.168.2.4│
└─────────┴─────────┴─────────┘
DNS: weba.company.com → 1.2.3.4
     webb.company.com → 1.2.3.4
     webc.company.com → 1.2.3.4

核心原理

  1. 公網 DNS 將所有子域名解析至防火牆公網 IP (1.2.3.4)。
  2. Apache 監聽 80/443,啟用 mod_proxy、mod_proxy_http、mod_rewrite、mod_ssl。
  3. 基於 Host 頭部 (域名) 匹配虛擬主機,反向代理至對應內網後端。
  4. 內網後端無需公網 IP,僅需允許防火牆內網 IP 訪問其 80/443 端口。
  5. Apache 重寫響應頭/內容中的內網 URL 為公網域名,實現透明代理。

Apache 編譯安裝關鍵參數 (源碼構建場景)

./configure \
  --prefix=/usr/local/apache \
  --enable-module=most \
  --enable-shared=max \
  --enable-proxy \
  --enable-proxy-http \
  --enable-proxy-balancer \
  --enable-rewrite \
  --enable-ssl \
  --enable-so \
  --with-mpm=event          # 高併發推薦 event MPM
make && make install

現代發行版直接安裝二進制包:apt install apache2 libapache2-mod-proxy-html / dnf install httpd mod_ssl,隨後 a2enmod proxy proxy_http rewrite ssl headers / systemctl enable --now httpd。

httpd.conf 核心虛擬主機配置

# 全局設置
ServerTokens Prod
ServerSignature Off
Timeout 60
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
LimitRequestBody 10485760

# 監聽
Listen 80
Listen 443

# 模組加載 (動態共享對象模式)
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule headers_module modules/mod_headers.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so

# 基於域名的虛擬主機
NameVirtualHost *:80
NameVirtualHost *:443

# === weba.company.com ===

    ServerName weba.company.com
    ServerAlias www.weba.company.com
    # 強制 HTTPS
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]



    ServerName weba.company.com
    ServerAlias www.weba.company.com

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/weba.company.com.crt
    SSLCertificateKeyFile /etc/ssl/private/weba.company.com.key
    SSLCertificateChainFile /etc/ssl/certs/ca-bundle.crt

    # 安全頭
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    Header always set X-Frame-Options DENY
    Header always set X-Content-Type-Options nosniff

    # 反向代理核心
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass / https://192.168.2.2/ retry=5 timeout=30
    ProxyPassReverse / https://192.168.2.2/
    # 重寫響應內容中的內網 URL (可選,視應用而定)
    # ProxyHTMLEnable On
    # ProxyHTMLURLMap https://192.168.2.2/ https://weba.company.com/

    # 訪問日誌
    CustomLog /var/log/apache2/weba_access.log combined
    ErrorLog /var/log/apache2/weba_error.log


# === webb.company.com / webc.company.com 同理複製修改 ServerName/ProxyPass 目標 ===

內網域名解析:/etc/hosts 或內網 DNS

# /etc/hosts (防火牆上)
192.168.2.2   weba.company.com
192.168.2.3   webb.company.com
192.168.2.4   webc.company.com
# 生產環境強烈建議部署內網權威 DNS (BIND/Unbound/PowerDNS),統一管理

SSL 證書管理

  • 單域名:Let's Encrypt certbot --apache -d weba.company.com 自動續期。
  • 通配符:certbot -d *.company.com --manual --preferred-challenges dns 需 DNS-01 驗證。
  • 企業內部 CA:統一簽發、分發、OCSP Stapling 配置 (SSLUseStapling On)。

進階功能:負載均衡與健康檢查


    BalancerMember https://192.168.2.2:80 route=node1
    BalancerMember https://192.168.2.5:80 route=node2
    ProxySet lbmethod=byrequests
    ProxySet stickysession=ROUTEID



    ServerName weba.company.com
    # ... SSL 配置 ...
    ProxyPass / balancer://weba_cluster/
    ProxyPassReverse / balancer://weba_cluster/

安全加固清單

  1. 禁用 ProxyRequests On (防止成為開放代理)。
  2. 限制管理接口:server-status/server-info 僅綁定內網管理 IP。
  3. 速率限制:mod_ratelimit、mod_security (WAF)。
  4. 隱藏版本信息:ServerTokens Prod、ServerSignature Off。
  5. 日誌審計:記錄 X-Forwarded-For、Host、請求體 (敏感接口)。
  6. 定期更新:Apache、OpenSSL、ModSecurity 規則集 (OWASP CRS)。

驗證與故障排查

apachectl configtest          # 語法檢查
systemctl reload apache2      # 平滑重載
curl -v -H "Host: weba.company.com" https://127.0.0.1/  # 本地測試 Host 路由
tail -f /var/log/apache2/weba_access.log
# 後端 502/503:檢查防火牆是否放行防火牆→後端端口、後端服務是否運行、ProxyPass 目標 IP/端口正確
# SSL 錯誤:openssl s_client -connect weba.company.com:443 -servername weba.company.com

總結

Apache 反向代理以「單公網 IP + 域名路由 + 內網後端」模式,極低成本實現多站點安全發布。核心在於:NameVirtualHost + ProxyPreserveHost On + ProxyPass/ProxyPassReverse + SSL 終結 + 內網 DNS/hosts 解析。生產環境建議引入 Let's Encrypt 自動化證書、ModSecurity WAF、Prometheus 監控、日誌集中分析,構建完整的邊緣網關能力。

Copyright © Windows教程網 All Rights Reserved