Configuring KeepAlive with Apache RewriteRule [P] Proxying

Published on Author gryzli

Apache can be used for proxying requests with mod_proxy to a certain backend/upstream server.

Here is how a simple proxy configuration looks:

ProxyPreserveHost On
RewriteEngine On
RewriteRule ^/(.*)$$1 [P]

The Problem

The problem with this configuration is that Apache will create new TCP session to backend ( for every new request it receives.

This might look as an innocent detail at first.

But if your server receives thousands of connections per minute, in a very short period of time, you will full-fill the TCP connection limit on the backend/upstream.

The TCP/IP stack can handle up to 65 000 parallel connections, giving the fact that each TCP session established has some timeout until it expires, you have some pretty good chances to fill this up on a busy server.


The Solution

The solution to this problem is to use KeepAlive between Apache and the backend server.


Apache mod_rewrite [P] flag is using the default mod_proxy forward worker (there are two default proxy workers – one for forward and one for reverse proxying).

The default apache mod_proxy workers do not use HTTP Keep-Alive or connection pooling.


In order to use Keep-Alive you need to create additional proxy worker, which can be done by adding the following configuration to your server:

<Proxy "">
  ProxySet connectiontimeout=240 timeout=1024

Now you will have working Keep-Alive between Apache and your backend !