Making Apache as a proxy

If your application needs one of the webpages served from external sites, you can configure Apache as a proxy and filter specific URL patterns to be forwarded to external site, and the rest to the tomcat serving your web application.

My requirements were:

1. Need a uniform/transparent way to integrate external sites into my web application.
2. There should not be any URL change in the browser or should not issue a redirect but mimic forward for a specific URL pattern.
3. Ability to append additional URL parameters before forwarding to external site. [eg: my id and password that the external site expects as URL parameters which I don't want to expose]


For that I can configure my apache server as follows:


NameVirtualHost *:9090
<virtualhost *:9090="">
ServerName localhost

ProxyRequests Off
ProxyPreserveHost On

<proxy *="">
Order deny,allow
Allow from all
</proxy>

RewriteEngine On
RewriteRule ^/map-service/loadMap.do(.*)  http://192.168.111.111/some-service/load.do?%{QUERY_STRING}&amp;someparam=aaa    [P] 
RewriteRule ^/map-service/(.*)  http://192.168.111.111/some-service/$1?%{QUERY_STRING}  [P]

</virtualhost> 

Note:

The parameter [P] is important as it makes the rewrite rule as Proxy forward else it behaves like a URL rewrite, sending location header to the browser.
%{QUERY_STRING} is a placeholder for input arguments
$1 - the first regex match group (.*) in our case. which matches any part of the remaing context path after /map-service/



No comments: