2

We have a fairly typical setup: client <-> apache2 (2.2.22) <-> backend (*).

Initially we were using mod_rewrite with the [P] flag in the simplest form:

<VirtualHost *:80>
  RewriteEngine On
  ProxyPreserveHost On
  RewriteRule ^(.*)$ http://localhost:8081$1 [P,L]
</VirtualHost>

However using this setup, even when the clients used keep-alive connections and were sending multiple http requests using one tcp connection to apache, apache was creating a new tcp connection to the backend for reach request.

I decided to try using mod_proxy directly instead:

ProxyPreserveHost On
ProxyPass / http://localhost:8081/

And to my surprise now the tcp connections between apache and the backend are re-used (which was my initial goal).

Why is that? Is there something in mod_rewrite to configure to enable keepalive backend connections?

(*) The backend is actually haproxy <-> multiple app servers, but I don't think it matters here.

adamw
  • 135
  • 1
  • 5

3 Answers3

4

mod_proxy does connection pooling. mod_rewrite doesn't.

Ansgar Wiechers
  • 4,197
  • 2
  • 17
  • 26
  • Is there a way to configure mod_rewrite to do connection pooling as well? Is it documented anywhere in the docs (I couldn't find anything)? – adamw Sep 03 '12 at 15:07
  • Again, `mod_rewrite` does **not** do connection pooling. – Ansgar Wiechers Sep 03 '12 at 17:31
  • Ok, got it, just wanted to make sure it's not configurable. It should be mentioned in the docs, though :) – adamw Sep 03 '12 at 19:56
  • See also my blog with a more detailed explanation of the problem: http://www.warski.org/blog/2012/09/unexpected-problems-with-apache-and-mod_rewrite-under-high-load/ – adamw Sep 19 '12 at 17:11
  • It's not about pooling, `mod_rewrite` uses `mod_proxy`'s "Default Reverse Proxy Worker", which doesn't reuse connections and can't be configured. See [mod_proxy docs](https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#workers). – schieferstapel Apr 30 '21 at 08:36
1

You can use http keep alive even if you do reverse proxy via rewriting, you just need to set keepalive to On via the ProxySet directive like this:

<Proxy "http://backend">
    ProxySet keepalive=On
</Proxy>

Source: https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxyset

  • You probably mean `enablereuse`, `keepalive` is actually TCP-keepalive. The naming is a bit misleading in the context of HTTP. This works anyway, because like this, a new Proxy worker is created and `enablereuse` is `On` by default. Otherwise, this is the correct answer. – schieferstapel Apr 30 '21 at 08:39
0

Using a pair of dummy ProxyPass directives as described in http://www.gossamer-threads.com/lists/apache/users/336740 allows the use of connection pooling with a RewriteRule.