How to Enable Keep-Alive for Apache Servers

Updated on May 10, 2023

When you have Keep-Alive enabled, your visitors’ browser knows that it only needs to establish a TCP connection and request all of the web files to be loaded once instead of multiple times. This helps in speeding up your website, which in terms provides a better user experience (UX) for the visitor. In this post, we will explain how you can enable Keep-Alive for your website and have all the benefits from it.

Table of Contents:

How does Keep-Alive work?

Every time a visitor requests a file from your server, there is communication between the visitor's browser and your server. Because the network connection remains open (alive) when Keep-Alive is enabled, the server only needs to authorize the request once rather than several times, which significantly reduces CPU utilization and network overhead.

Keep-Alive is typically enabled by default on web servers. It's not always the case, though. For reasons related to server speed (their servers, not your website), some hosting firms disable keep-alive. To check the keep-alive status of your domain, use a website speed tester or a keep-alive checker.

How to Enable Keep-Alive for Apache Servers

Whether you run a Drupal, WordPress, Joomla, or any other sort of CMS website, there are several methods you may enable Keep-Alive for Apache. The simplest approach to enable HTTP Keep-Alive connections is through altering the .htaccess file, but you can also do it using server-side access (you need access to your server or droplet for this). You can find the .htaccess method below.

Edit the .htaccess File

To enable Keep-Alive through .htaccess, you need to add the following code to your .htaccess file:

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

The benefit of employing this method is that it overrides any modifications made to your server's configuration file. Therefore, you can enable it without requiring server-side access. After making changes to the .htaccess file, always test your website for bugs or malfunctions to avoid mistakes or situations where it doesn't work out as you want it to (in which case you should not use this method). Keep the updated settings if they work. Remove the lines from the .htaccess file if it doesn't.

Adjust Apache Settings

Every time you install Apache from scratch, Keep-Alive should be configured automatically on a Unix (Linux) server. If it isn't already enabled, it is simple to do so by making the following changes to the httpd.conf configuration file for Apache:

  • KeepAlive: Set it to "KeepAlive on" to enable Keep-Alive. To disable it, use "KeepAlive off".
  • MaxKeepAliveRequests: Sets the maximum number of requests for every keep-alive connection. A good number to start with is 60 requests per connection.
  • KeepAliveTimeout: Sets how long your server should wait for new requests from clients. The 15-second default is too high. Set it to 3 seconds to start with and gradually go up when it underperforms. Setting it to a lower amount of seconds results in fewer parallel connections, which again results in less server load.

Note: When you can't find the http.conf file type execute find / -name httpd.conf via the command-line.

Conclusion

There is no downside to enabling HTTP Keep-Alive when it comes to improving your website speed. Using a Keep-Alive connection will almost always speed up your website.

We hope you find this article useful. Discover more about FastCloud - the top-rated Hosting Solutions for personal and small business websites in four consecutive years by the HostAdvice Community!

SSD Cloud Hosting

  • Free Domain Transfer
  • 24/7 Technical Support
  • Fast SSD Storage
  • Hack-free Protection
  • Free Script Installation
  • Free Website Transfer
  • Free Cloudflare CDN
  • Immediate Activation
View More