Knowledge Base

How to use keep-alive connections to improve performance

This article describes how to use keep-alive connections in Apache to help improve performance.

Using keep-alive connections

Keep-alive connections use a single TCP connection to handle multiple HTTP requests and responses. Instead of opening a new TCP connection every time a brower sends a request, keep-alive connections enable clients and servers to communicate more efficiently and use fewer resources.

Many performance testing sites, such as GTMetrix, check if keep-alive connections are enabled for a site. If they are disabled, you receive a lower site performance score.

If keep-alive conections are disabled for your site, you can easily enable them. To do this, follow these steps:

  1. Edit or create an .htaccess file in your site's document root directory.
  2. Copy the following lines and paste them into the .htaccess file:
    <IfModule mod_headers.c>
        Header set Connection keep-alive
    </IfModule>
    
  3. Save your changes to the .htaccess file. Keep-alive connections are now enabled.

    To verify that keep-alive connections are enabled, you can examine the raw HTTP headers sent between the browser and web server. To do this, use a browser plugin that displays the raw headers such as Live HTTP headers for Mozilla Firefox, or the Developer Tools feature in Google Chrome. When keep-alive connections are enabled, Apache adds the following line to the HTTP response header:

    Connection: Keep-Alive

 

More Information

For more information about keep-alive connections, please visit https://en.wikipedia.org/wiki/HTTP_persistent_connection.