萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Apache HTTP Server 反向代理與端口映射配置大全:ProxyPass、SSL 終結、Cookie 路徑重寫

核心模組啟用

# httpd.conf 或模組載入檔
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so  # WebSocket
LoadModule rewrite_module modules/mod_rewrite.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

場景一:本地端口映射 (Port Mapping)

將 /yingshe 路徑代理至後端 https://192.168.10.12:8080/

# 虛擬主機上下文
DocumentRoot "E:/www/yingshe"
<Directory "E:/www/yingshe">
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
    DirectoryIndex index.php
</Directory>

# 反向代理核心指令
ProxyPass /yingshe https://192.168.10.12:8080/
ProxyPassReverse /yingshe https://192.168.10.12:8080/

<Proxy https://192.168.10.12:8080/>
    AllowOverride None
    Require all granted
</Proxy>
  • ProxyPass:將請求轉發至後端。
  • ProxyPassReverse:重寫回應中的 Location、Content-Location、URI 標頭,確保重導向指向代理路徑而非後端真實路徑。
  • <Proxy>:授權存取控制 (2.4+ 語法 Require all granted)。

場景二:HTTPS 反向代理 (SSL 終結 / SSL Passthrough)

A. 客戶端 HTTPS → Apache → 後端 HTTPS (雙向 SSL)

Listen 8002
<VirtualHost *:8002>
    ServerName proxy.example.com
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/proxy.crt
    SSLCertificateKeyFile /etc/ssl/private/proxy.key

    SSLProxyEngine on              # 啟用代理端 SSL
    SSLProxyVerify none            # 不驗證後端憑證 (內網可信)
    SSLProxyCheckPeerName off      # 不檢查後端憑證 CN
    # SSLProxyCACertificateFile /path/to/ca.crt  # 若需驗證

    ProxyRequests Off              # 關閉正向代理
    ProxyPass / https://192.168.10.201/
    ProxyPassReverse / https://192.168.10.201/

    # 關鍵:Cookie Path 重寫,解決 Session 丟失
    ProxyPassReverseCookiePath /https://192.168.10.201/ /

    <Proxy https://192.168.10.201/>
        Require all granted
    </Proxy>
</VirtualHost>

B. 客戶端 HTTP → Apache → 後端 HTTPS (SSL 終結於 Apache)

Listen 80
<VirtualHost *:80>
    ServerName proxy.example.com
    # 可選:強制跳轉 HTTPS
    # RewriteEngine On
    # RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

    SSLProxyEngine on
    ProxyPass / https://192.168.10.201/
    ProxyPassReverse / https://192.168.10.201/
    ProxyPassReverseCookiePath /https://192.168.10.201/ /

    <Proxy https://192.168.10.201/>
        Require all granted
    </Proxy>
</VirtualHost>

C. 客戶端 HTTPS → Apache → 後端 HTTP (SSL Offloading)

Listen 443
<VirtualHost *:443>
    ServerName proxy.example.com
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/proxy.crt
    SSLCertificateKeyFile /etc/ssl/private/proxy.key

    ProxyRequests Off
    ProxyPass / https://192.168.10.201/
    ProxyPassReverse / https://192.168.10.201/
    # 後端為 HTTP,無 Cookie Path 問題,除非後端設定了 Secure Cookie

    <Proxy https://192.168.10.201/>
        Require all granted
    </Proxy>
</VirtualHost>

場景三:負載均衡與高可用

<Proxy balancer://mycluster>
    BalancerMember https://192.168.10.11:8080 loadfactor=3
    BalancerMember https://192.168.10.12:8080 loadfactor=2
    BalancerMember https://192.168.10.13:8080 status=+H  # 熱備
    ProxySet lbmethod=byrequests
    ProxySet stickysession=JSESSIONID|jsessionid  # Session 親和性
</Proxy>

ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/

場景四:WebSocket 代理

ProxyPass /ws/ ws://backend:8080/ws/
ProxyPassReverse /ws/ ws://backend:8080/ws/
# 需 mod_proxy_wstunnel
# 自動處理 Upgrade: websocket 握手

常見問題與最佳實踐

問題原因解決
後端重導向至內部 IP/埠口後端感知自身真實位址ProxyPassReverse + ProxyPreserveHost On (傳遞原 Host 標頭)
Session/Cookie 失效Cookie Path/Domain 不匹配代理路徑ProxyPassReverseCookiePath /backend-path /proxy-path
ProxyPassReverseCookieDomain backend.domain proxy.domain
大檔案上傳截斷/超時代理緩衝/超時限制proxy_request_buffering off; proxy_max_temp_file_size 0; proxy_read_timeout 300;
WebSocket 連線失敗缺少 Upgrade 標頭轉發啟用 mod_proxy_wstunnel,配置 ws:// 協議
後端憑證自簽名導致代理失敗SSLProxyVerify 預設 requireSSLProxyVerify none 或提供 CA 憑證

效能調優參數

# 連線池複用
ProxyPass / balancer://mycluster/ retry=5 timeout=30 keepalive=On

# 緩衝控制
proxy_buffering on
proxy_buffer_size 16k
proxy_buffers 4 32k
proxy_busy_buffers_size 64k

# 緩存 (需 mod_cache + mod_cache_disk)
CacheEnable disk /
CacheRoot /var/cache/apache2/proxy
CacheMaxFileSize 104857600
CacheMinFileSize 1024

安全加固

  • ProxyRequests Off:**嚴禁開啟正向代理**,否則成為開放代理被濫用。
  • <Proxy> Require ip 10.0.0.0/8:限制只允許內網存取代理端點。
  • 啟用 mod_security WAF 規則過濾惡意請求。
  • 定期更新 OpenSSL、Apache 版本修補 CVE。
copyright © 萬盛學電腦網 all rights reserved