Apache-2.4

帶有重寫模組的apache反向代理

  • June 20, 2019

我正在使用 rewrite mod 在我的 apache 反向代理上配置一個新的虛擬主機,在測試配置時一切正常,但只有一個頁面(圖像頁面)不起作用。點擊圖像頁面時,我會在瀏覽器中獲取本地 URL(https://localserver:9251/share/collab?collabSession=ea4a80bf-6c19-473b-ad08-bdce391dbe19&type=join&user=username&isInitiator=true)。

埠 9251、433 和 80 在 ports.conf 中啟動

在我的虛擬主機配置下方:

<VirtualHost *:80>
   ServerName publicdomaine.com
   ServerAlias www.publicdomaine.com
   Redirect Permanent / https://publicdomaine.com
</VirtualHost>

<VirtualHost *:443>
   ServerName publicdomaine.com
   ServerAlias www.publicdomaine.com

   <IfModule mod_proxy.c>
       SetEnv proxy-nokeepalive 1

       SetEnv proxy-initial-not-pooled 1
       ProxyRequests Off
       ProxyPreserveHost Off

       ProxyPass / https://localserver/
       ProxyPassReverse / https://localserver/
   </IfModule>

   SSLEngine on
   SSLProxyEngine On
   SSLProxyVerify none
   SSLProxyCheckPeerCN off
   SSLProxyCheckPeerName off
   SSLProxyCheckPeerExpire off
   SSLCertificateKeyFile /etc/apache2/cert/mykey.pem
   SSLCertificateFile /etc/apache2/cert/mycert.pem
   SSLCertificateChainFile /etc/apache2/cert/bundle.crt

   RewriteEngine On
   LogLevel alert rewrite:trace6

   RewriteCond %{QUERY_STRING} ^(.*)&studyUid=(.*)
   RewriteCond %{QUERY_STRING} ^host=publicdomaine.com(.*)$
   RewriteRule ^/ris/web/imageshare/startSession - [E=SUI_VAL:%1]

   RewriteRule /ris/web/imageshare/startSession https://localserver/ris/web/imageshare/startSession?host=cimcsp1.cimy.local%{ENV:SUI_VAL} [P,QSA,L]

   ErrorLog /var/log/signoff-error_log

   CustomLog /var/log/signoff-access_log combined
</VirtualHost>

<VirtualHost *:9251>
   SetEnv proxy-nokeepalive 1
   SetEnv proxy-initial-not-pooled 1
   ProxyRequests Off
   ProxyPreserveHost Off

   ProxyPass / https://localserver:9251/
   ProxyPassReverse / https://localserver:9251/
</VirtualHost>

我觀察到與 fiddler 的 http 交換。消息交換:

GET https://publichostname/ris/web/imageshare/startSession?host=publichostname&studyUid=1.3.51.0.1.1.10.233.24.237.333827.7779290494 HTTP/1.1

反向代理將參數 host 重寫為 localserver :

GET https://publichostname/ris/web/imageshare/startSession?host=localserver&studyUid=1.3.51.0.1.1.10.233.24.237.333827.7779290494 HTTP/1.1

響應是 JSON:

{“initiatorLink”:“https://localserver:9251/share/collab?collabSession=74bea62c-474a-4ee5-9d74-e793fef66738&type=join&user=username&isInitiator=true”,“studyUid”:“1.3.51.0.1.1.10.233. 24.237.333827.7779290494”,“baseLink”:“https://localserver:9251/share/?collabSession=74bea62c-474a-4ee5-9d74-e793fef66738”,“closedLink”:null}

這個回复讓我知道:

CONNECT localserver:9251 HTTP/1.1
Host: localserver:9251

這應該是通過反向代理將我隧道到:

CONNECT publichostname:9251 HTTP/1.1
Host: publichostname:9251

找到了 !!!我添加了替代模組來修改伺服器響應,它的工作!!!!!!!命令補充說:

AddOutputFilterByType SUBSTITUTE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript application/json
Substitute "s|https:\/\/localserver:9251|https:\/\/publichostname:9251|ni"

感謝您的幫助!!!!

最好的選擇是配置您的應用程序伺服器以生成與代理一起使用的正確 URL。

但是,如果您不能這樣做,您可以使用另一個 Apache 模組,mod_proxy_html. 該模組提供了指令ProxyHTMLURLMap,您可以使用它來重寫響應。

ProxyPass / https://localserver/
ProxyPassReverse / https://localserver/
SetOutputFilter proxy-html # make sure the output is filtered by proxy-html
ProxyHTMLURLMap https://localserver:9251/ https://publichostname:9251/
ProxyHTMLExtended On       # by default only HTML output is filtered

這應該使用 publichostname URL 重寫答案中所有出現的 localserver URL。

請注意,這會給網路伺服器帶來額外的負載,因為它必須分析和修改每個答案,而不僅僅是傳遞答案。正確配置應用程序伺服器將是首選方法。

引用自:https://serverfault.com/questions/970373