由于需要在windows系統上實(shí)現一個(gè)反向代理功能,因此就考慮到使用apache。
Apache具有反向代理的功能。通過(guò)對文件httpd.conf,進(jìn)行簡(jiǎn)單的設置,即可以實(shí)現反向代理功能。但是被代理的服務(wù)中,如果包含有絕對路徑的話(huà),代理設置則無(wú)效。如Apache中的相關(guān)文檔說(shuō)明:被代理的頁(yè)面中的所有絕對路徑的連接都會(huì )突破代理機制而直接取得。
也即,不能在被代理的服務(wù)中包含有絕對路徑。而且,經(jīng)過(guò)我的測試,在下面三類(lèi)地址中,類(lèi)似于
- 第1類(lèi),<a href="test/index2.jsp">test</a>
- 第2類(lèi),<a href="<%=request.getContextPath()%>/index2.jsp">backToIndex2</a>
- 第3類(lèi),<a href="http://10.1.1.1/internal1/test/index2.jsp">AllPathTest</a>
第1類(lèi),<a href="test/index2.jsp">test</a>第2類(lèi),<a href="<%=request.getContextPath()%>/index2.jsp">backToIndex2</a>第3類(lèi),<a >AllPathTest</a>
如果我的設置信息如下:
- <IfModule mod_proxy.c>
- ProxyRequests Off
- ProxyPass /test1/ http://10.1.1.1/
- <Location /test1/>
- ProxyPassReverse /
- </Location>
- </IfModule>
<IfModule mod_proxy.c>ProxyRequests OffProxyPass /test1/ http://10.1.1.1/<Location /test1/>ProxyPassReverse /</Location></IfModule>
則只有第1類(lèi)地址能夠正常跳轉,顯示正確內容,而第2,3類(lèi)地址則不行。
但是如果我的設置文件修改為:
- <IfModule mod_proxy.c>
- ProxyRequests Off
- ProxyPass / http://10.1.1.1/
- <Location />
- ProxyPassReverse /
- </Location>
- </IfModule>
<IfModule mod_proxy.c>ProxyRequests OffProxyPass / http://10.1.1.1/<Location />ProxyPassReverse /</Location></IfModule>
則只有第3類(lèi)地址不能正常跳轉。第1,2類(lèi)能夠正常跳轉。
在第2次配置中,是因為把地址http://10.1.1.1/映射為/,所以第2類(lèi)地址才能正確顯示。但實(shí)際上,如果使用apache作為反向代理,至少會(huì )映射到內網(wǎng)的多個(gè)地址會(huì )映射,因此不可避免,需要使用類(lèi)似于第1次中的配置,如果這樣的話(huà),那就是說(shuō)反向代理不能處理第2,3類(lèi)地址了。
我不知道目前使用apache作為反向代理的實(shí)例多不多。我覺(jué)得,既然有這樣對地址的嚴格限制,想輕松的使用起來(lái)并不簡(jiǎn)單。
如何解決反向代理的絕對路徑問(wèn)題呢?
經(jīng)過(guò)一翻google,發(fā)現搜索到的內容基本上如apache文檔所說(shuō),不適用于絕對路徑的情況。但還是找到了一條很有用的信息,mod_proxy_html模塊。鑒于目前的google中文搜索結果中,基本上沒(méi)有提到mod_proxy_html和解決反向代理絕對路徑的問(wèn)題。因此記下這個(gè)設置過(guò)程,希望能對其他人有所幫助。
mod_proxy_html模塊:提供在反向代理過(guò)程中,重寫(xiě)HTML links的功能。
最新的mod_proxy_html版本為3.0.1,在此我使用的是mod_proxy_html3.0.0版本,注意:它與之前的mod_proxy_html2.5版本有較大的區別。從3.0版本開(kāi)始,使用了一個(gè)獨立的配置文件proxy_html.conf。
環(huán)境:window 平臺中使用apache實(shí)現反向代理。
1,下載Apache2.2,安裝。
2,下載mod_proxy_html-3.0.0-w32.zip。
3,下載并安裝the Visual C++ 2005 SP1 Redistributable Package (the binary is build with VC 2005 SP1),下載地址:
http://www.microsoft.com/downloads/details.aspx?FamilyID=200b2fd9-ae1a-4a14-984d-389c36f85647&DisplayLang=en
4,新建文件夾.../apache2/modules/mod_proxy_html/ 并復制mod_proxy_html.so和
libxml2.dll到該文件夾。
5,復制httpd.exe.manifest文件到.../apache2/bin中。
6,復制proxy_html.conf到.../apache2/conf中。
7,修改配置文件httpd.conf:
在LoadModule的配置中,去掉與proxy有關(guān)的模塊的注釋?zhuān)慈サ?符合,
去掉LoadModule headers_module modules/mod_headers.so的注釋?zhuān)?
8,在httpd.conf中添加:
LoadModule proxy_html_module modules/mod_proxy_html/mod_proxy_html.so
Include conf/proxy_html.conf
9,再對proxy_html.conf進(jìn)行相關(guān)的設置:
- <IfModule mod_proxy.c>
- ProxyRequests Off
- ProxyHTMLExtended On
- ProxyPass /test1/ http://10.1.1.1/
- <Location /test1/>
- ProxyPassReverse /
- ProxyHTMLURLMap http://10.1.1.1 /test1
- SetOutputFilter proxy-html
- ProxyHTMLURLMap / /test1/
- RequestHeader unset Accept-Encoding
- </Location>
- </IfModule>
<IfModule mod_proxy.c>ProxyRequests OffProxyHTMLExtended OnProxyPass /test1/ http://10.1.1.1/<Location /test1/>ProxyPassReverse /ProxyHTMLURLMap http://10.1.1.1 /test1SetOutputFilter proxy-htmlProxyHTMLURLMap / /test1/RequestHeader unset Accept-Encoding</Location></IfModule>
重新啟動(dòng)apache,即可。
此時(shí)apache中就包含了mod_proxy_html模塊。
最重要的設置元素:
ProxyHTMLURLMap http://10.1.1.1 /test1
設置返回的html內容的重寫(xiě)規則,使用/test1代替http://10.1.1.1,此時(shí)即可以全部處理上述所說(shuō)的三種類(lèi)型的地址了。