Forum Moderators: phranque

Message Too Old, No Replies

Apache rewrite and proxy to remote Tomcat Worker

         

egPDX

5:45 pm on Oct 15, 2007 (gmt 0)

10+ Year Member



Hi all. I've setup Apache 2.0.59 (BoxA) as a proxy to a remote Tomcat 5.5.25 worker (BoxB) via mod_proxy using

ProxyPass / [<TomcatWorkerIP>:<PORT>...]
ProxyPassReverse / [<TomcatWorkerIP>:<PORT>...]

Now I'd like to simplify the user login URL from
[dnsname...]
to
[dnsname...]

I've attempted to do this via mod_rewrite but am having problems. What I'd like to do is have the Apache rewrite occur first (BoxA), then have the results sent to the remote Tomcat worker (BoxB), all the while maintaining the requesting URL (http://dnsname/login).

I tried the following (httpd.conf):

RewriteEngine On
RewriteLog logs/rewrite.log
RewriteLogLevel 9
RewriteRule ^/login /appname/servlet/login [P]

<VirtualHost *:<PORT>
ServerName dnsname
ProxyRequests Off
ProxyPass / [<TomcatWorkerIP>:<PORT>...]
ProxyPassReverse / [<TomcatWorkerIP>:<PORT>...]
Errorlog logs/appname_access.log
</VirtualHost>

The above configuration works initially, but then fails on subsequent attempts.

Any ideas or thoughts? Thanks in advance.

jdMorgan

12:50 pm on Oct 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These reverse-proxy and module-execution-order problems are always confusing, so I hope I get this right -- or at least close.

First, be aware that you can't easily control module execution order on Apache 2.x, and even though you can control it on Apache 1.x, modifying the LoadModule list can often create unwanted side-effects. And even if it doesn't, you're still left with a solution that won't migrate to Apache 2.x.

The simplest solution is to use an intermediate URL-path, so that the ProxyPass directive is not invoked until after the rewrite. This can be done in many ways, but a simple example would be:


ProxyPass /proxy/
...
RewriteRule ^/login /proxy/appname/servlet/login [PT,L]

Here, only URL-paths beginning with /proxy/ are recognized by ProxyPass, and the RewriteRule is modified to add that path-part. So the RewriteRule must be executed before mod_proxy will do anything. I also changed the flag to [PT] so that mod_rewrite will output the new path in 'URL format' instead of 'filename format' -- This is required if mod_proxy is to recognize the path (See mod_rewrite RewriteRule [PT] flag documentation).

With the above in mind, review the mod_proxy and mod_rewrite documentation; I can't be sure I got this right, and there are of course many possible subtleties and interactions that you need to watch out for. The mod_proxy examples for reverse-proxy setup and the descriptions of the [P] and [PT] RewriteRule flags should be checked at a minimum.

Jim