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
mod_proxy、mod_proxy_http、mod_rewrite、mod_ssl。Host 頭部 (域名) 匹配虛擬主機,反向代理至對應內網後端。./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。
# 全局設置
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 (防火牆上)
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),統一管理
certbot --apache -d weba.company.com 自動續期。certbot -d *.company.com --manual --preferred-challenges dns 需 DNS-01 驗證。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/
server-status/server-info 僅綁定內網管理 IP。mod_ratelimit、mod_security (WAF)。ServerTokens Prod、ServerSignature Off。X-Forwarded-For、Host、請求體 (敏感接口)。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 監控、日誌集中分析,構建完整的邊緣網關能力。