Skip to content
Eduardo Lira

INFRAESTRUTURA · DESEMPENHO

Keep-Alive on Apache: what the 2012 .htaccess line does, and where the feature is configured

In 2012 one line in .htaccess made my site faster. Looking back: what that line does, where Keep-Alive is configured in Apache and how to check it with curl.

In September 2012 I posted a tip here to speed up sites on Apache: put these lines in .htaccess.

<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>

After the change, the site got faster right away, and I wrote about it with surprise (“frighteningly”). The explanation of why Keep-Alive speeds things up was right: with it, the browser opens one TCP connection and requests several files through it, instead of opening a connection for each file. What I hadn’t understood was the role of that line.

What the line does

Header set only writes a header into the response. The response starts saying Connection: keep-alive, but whether the connection stays open afterwards is up to the server, and the server doesn’t read that header to decide. If Apache is configured to close the connection, it closes it, with or without the line.

That doesn’t mean the gain I saw didn’t happen. I no longer have that server to know what else weighed on that result. What can be said is that the line alone doesn’t turn the feature on: if Keep-Alive is off in Apache, the connection is closed with or without it.

Where Keep-Alive is configured

These are Apache core directives, in the server configuration file (httpd.conf, apache2.conf or the virtual host file):

KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

KeepAlive On is already the default, and the other two values above are the defaults as well. These directives are not accepted in .htaccess. On shared hosting, the hosting company decides, and it sometimes turns it off or shortens the timeout to serve more customers per server.

How to really check

Request two pages from the same site in a single curl run and see whether it reuses the connection:

curl -sv -o /dev/null -o /dev/null https://yoursite.com/ https://yoursite.com/robots.txt 2>&1 | grep -i "connection #\|reusing\|re-using"

With Keep-Alive, both requests go through the same connection: curl shows Connection #0 for both and, on the second, a Reusing existing connection line (Re-using in older versions). Without Keep-Alive, the second request shows up as Connection #1.

In the browser, the Network tab of the developer tools has a “Connection ID” column (in Chrome, right-click the table header to add it). Several files with the same number went through the same connection.

What has changed since 2012

In HTTP/1.1 the connection is already persistent by default; it is the Connection: close header that asks to close it. In HTTP/2 (and HTTP/3), which a good share of sites already use, the browser opens one connection per site and sends all requests through it at the same time, and the Connection header isn’t even allowed in the response. On a site like that, the line has no effect.

If your site has that line in .htaccess, what tells you whether Keep-Alive is working is the curl test above, not the line being there.

References

Who writes this

Eduardo Lira

Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.

Found a mistake in this post?

A command that no longer works, a broken link, wrong information: tell me what you saw. The message goes only to the author and is not shown on the site.