Tomcat – mod_proxy incorrect redirect behaviour

apache-2.2mod-proxytomcat

In chrome this configuration causes an infinite redirect loop and in every other browser I have tried a request for https://www.example.com/servlet/foo is resulting in a redirect to https://www.example.com/foo/ instead of https://www.example.com/servlet/foo/ however this only occurs when I do not include a trailing / at the end of the request url (i.e. http://www.flightboard.net/servlet/foo/ works just fine).

<VirtualHost *:80>
    # ...

    RewriteEngine On

    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} ^/servlet(/.*)?$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    # ...

    ProxyPass /servlet/ ajp://localhost:8009/
    ProxyPassReverse /servlet/ ajp://localhost:8009/
</VirtualHost>

The virtual host on port 443 has no rewrite rules that could possibly causing the problem, the tomcat contexts being referenced do not send any redirects, and if I change the ProxyPass and ProxyPassReverse directives to:

    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/

everything works fine (except for the fact everything from www.example.com is being passed to the proxy which is not the behaviour I want). I'm fairly certain this is a problem with the way I have my proxy settings configured because I did log all the rewrite output coming from apache and it was all correct.

Best Answer

Switching from mod_proxy_ajp to mod_proxy_http seems to resolve this problem.

ProxyPass /servlet/ http://localhost:8080/
ProxyPassReverse /servlet/ http://localhost:8080/

Unfortunately this has the side effect of not passing along the ip address of the remote connection. Another alternative is to add a rewrite rule to match the url with the missing slash at the end:

RewriteCond %{REQUEST_URI} ^/servlet/[^/]+$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]