Apache 2.2: RewriteMap, ProxyPass interpolation and Keep-Alive

Posted on

Apache 2.2: RewriteMap, ProxyPass interpolation and Keep-Alive – Problems with loading a website are often blamed on the Internet connection, but even the most perfectly set up network cannot help if there is no service to reply at your destination. One of the most popular HTTP servers used for this task is Apache2. Much of Apache’s popularity can be attributed to its easy installation and use, but never the less it is possible to run into problems with even the easiest of the software. If you’ve encountered an issue loading your web page, follow these simple troubleshooting methods outlined in this guide to attempt to get your web server back up and working again. Below are some tips in manage your apache2 server when you find problem about apache-2.2, mod-rewrite, mod-proxy, keep-alive, .

I have an Apache httpd used as a SSL reverse proxy frontend for a lot of backends.
I compute the name of the backend from the path given by the user.
e.g: https://myhostname.com/myaccount translates to http://myaccount.myhostname.com

For this I use a RewriteMap and RewriteRule with [P] flag on. Everything works fine.

However I now need a persistent connection between the reverse proxy and the backend. I am well aware that keep-alive doesn’t work with RewriteRule‘s [P] (http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_p – performance warning) therefore I tried to use ProxyPass and environment variable interpolation to do the work.
I got a RewriteRule that don’t do nothing except saving the map result into an environment variable and then call the ProxyPass using this variable.

RewriteMap mymap prg:/path/to/my/map
RewriteCond %{REQUEST_URI} ^/([^/]+)/?.*$
RewriteCond ${mymap:%1} ^http://([^/]+/)$
RewriteRule ^/([^/]+)/?$ - [L,E=original:$1,E=rewritten:%1]

ProxyPassInterpolateEnv On
ProxyPassReverse /${original}/ http://${rewritten} interpolate

This result in the same behaviour as RewriteRule with [P]: the proxying is ok but don’t uses http keep-alive.

I tried this with a hard-coded hostname instead of using ${rewritten} (but keeping the ${original}) and observed that it’s the part that make Apache creating new connection instead of reusing an existing one.

This bug: https://issues.apache.org/bugzilla/show_bug.cgi?id=43308 is raising the same issue and propose a patch that may solve my problem.

However, I wanted to know if anyone had an idea to solve this in a different/better way.

When using the [P] flag in a RewriteRule to proxy, you would normally be able to use the ProxySet directive to set the options normally configured via ProxyPass.

But in this case you do not have a specific proxy URL to configure. If you are only proxying user accounts, you could do something like:

ProxyPassMatch /w+/ http://$1.myhostname.com/ keeplive=on

Although from your example it seems it’s possible different domains are in use (hence the use of a RewriteMap).

Alternatively, another thought that just occurred, it might work to use the following (untested):

<Proxy *>
    ProxySet keepalive=on
</Proxy>

Leave a Reply

Your email address will not be published.