# 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
將 /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)。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>
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>
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/
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-pathProxyPassReverseCookieDomain 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 預設 require | SSLProxyVerify 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 規則過濾惡意請求。