If you’re trying to optimize your website’s performance then one of the ways you can do that is to enable HTTP keep alive headers or Persistent Connections. The reason for this is to prevent an HTTP keep alive timeout. What happens is that it keeps the client/server connection open across multiple server requests.
This connection allows the server to retrieve content for a web page across multiple server requests thereby improving performance (it is easier to serve content from several places than to try and serve one large file). By not enabling HTTP keep alive, the server also has to open a new connection for each request.
Why Enable HTTP Keep Alive?
GTMetrix lists this as a high priority and recommends that the feature be enabled in order to improve your performance score.
Enabling HTTP Keep-Alive or HTTP persistent connections allow the same TCP connection to send and receive multiple HTTP requests, thus reducing the latency for subsequent requests.
How to Enable HTTP Keep Alive Header
Keep alive is enabled using the “Connection: Keep Alive” HTTP header. When this is not enabled your server returns a “connection: closed”. In order to take advantage of the feature you will need to have access to certain files. The steps required to enable the performance enhancing setting is different for each of the varying stup that your website could possibly have. Nonetheless, the steps necessary to enable the HTTP keep alive header on the various configurations are outlined below.
How to Enable Keep Alive Using htaccess
In order to use this step, you must have access to your .htaccess file which is found in the root folder of your WordPress install. If you’re using Cpanel then you can gain access to the file via File Manager. If not, you can use an FTP client to locate the file, download it, edit it and reupload it.
.htaccess Enable Keep Alive
This is the code that you will need to paste in the .htaccess file in order to enable Keep Alive:
<IfModule mod_headers.c> Header set Connection keep-alive </IfModule>
Now, you should paste the code directly below “#END WordPress” Please be sure to paste it only after this line or you WILL break your website which will return a 500 server error.
Once you’ve pasted the code in the correct location, save the file and, if you have to reupload, do so now. By adding the code to your .htaccess file you will add keep alive headers to all of your requests, which will override most web server or host limitations, allowing you to achieve greater speed and overall performance.
How to Enable keep-alive in Apache
If you’re using Apache then the process is significantly different. Here’s what you need to do enable apache keep-alive header. The first thing you will need to do is:
- Edit this file
- Then run the following command: sudo vim /etc/httpd/conf/httpd.conf
- Now add this in the file (Any line should do the trick): KeepAlive On
- In order for the new settings to take effect you will need to restart your
httpdservice by performing the following command: sudo service httpd restart
That’s it for the Apache keep alive configuration. You now have
KeepAlive enabled on your server and it will now improve your results in the speedtests and also improve user experience.
How to Enable keep-alive in NGINX
If you’re using NGINX then the steps are a bit simpler than the rest. You should be aware that by default you can get insights on Keep Alive issues via the HttpCoreModule. You should check for “keepalive_disable” and remove this line in order to enable the feature. However, you should first know why keep-alive is being disabled before you go tinkering with the NGINX settings.
How to Test Keep Alive
If you want to be sure that the feature is enabled on your server then you can use simple online tools to check the status. You can use GTmetrix to test the feature among other performance enhancements or you can use Gift of Speeds Keep-Alive Checker.
While Keep Alive is normally enabled by default on most hosting environments, you should be aware that, if you’re using shared hosting, your host might disable the feature. It is always wise to check and enable the feature yourself. Any of the above steps (relative to your configuration) will override your hosting limitation.