Tutorial How to enable keep alive
What is keep alive?
In this tutorial we will discuss about How to enable keep alive and what is it.
- Keep alive is a method to allow the same tcp connection for HTTP conversation instead of opening a new one with each new request.
- More simply put, it is a communication between the web server and the web browser that says "you can grab more than just one file at a time".
- Keep alive is also known as a persistant connection
How to enable keep alive ?
- Enable keep-alive using .htaccess
Place .htaccess file with this content
SetEnv KeepAlive On
SetEnv KeepAliveTimeout 100
SetEnv MaxKeepAliveRequests 500
Header unset Connection
Header set Connection keep-alive
Header unset Keep-Alive
Header set Keep-Alive timeout=100,max=500
- Enable keep-alive in Apache
If you are able to access your Apache config file, you can turn on keep-alive there. The applicable sections are shown below. Open file /etc/httpd/conf/httpd.conf and change following values as shown below :
[root@Myserver]# nano /etc/httpd/conf/httpd.conf
And then save and restart Apache service using following command and you have successfully enabled Keep-Alive in your Apache server.
[root@Myserver]# service httpd restart
[root@Myserver]# /etc/init.d/httpd restart
- Enable keep-alive in NGINX
Keepalive connections can have a major impact on performance by reducing the CPU and network overhead needed to open and close connections. NGINX terminates all client connections and creates separate and independent connections to the upstream servers. NGINX supports keepalives for both clients and upstream servers. The following directives relate to client keepalives:
keepalive_requests – The number of requests a client can make over a single keepalive connection. The default is 100, but a much higher value can be especially useful for testing with a load-generation tool, which generally sends a large number of requests from a single client.
keepalive_timeout – How long an idle keepalive connection remains open.
keepalive – The number of idle keepalive connections to an upstream server that remain open for each worker process. There is no default value.
To enable keepalive connections to upstream servers you must also include the following directives in the configuration:
proxy_set_header Connection "";
Why is keep-alive used?
In order to display webpages a browser must request files from a web server somewhere. There is a brief communication where the browser asks for a file and the web server says yes or no.
Webpages are often a collection of many files and if a new connection (the brief communication) has to be opened for each and everyone of those files it could take significantly longer to display that webpage.
When keep alive is not enabled this process can increase the time it takes to download the page and waste server resources.