KeepAlive Directive is used to enables the HTTP Persistence connections.
This is the persistance connection features of HTTP/1.1 which provides the long lived HTTP session which can be sent over same TCP connection.
Using KeepAlive is closely related to the MaxClients setting.
Note: In some cases 50% improvement in latency time for HTML document with more images.
Syntax for KeepAlive
[cc lang=”apache”]
KeepAlive On|Off
[/cc]
Default value for KeepAlive
[cc lang=”apache”]
KeepAlive On
[/cc]
How to enable KeepAlive?
[cc lang=”apache”]
KeepAlive On
[/cc]
Note: A Keep-Alive connection with HTTP/1.0 can only be used if length of the content known in advance.
How it works?
Basically KeepAlive keeps each HTTP connection open for short time to receive the follow up request from the same connection. This is because we can consider that client will try to make a several connection to our web server as a follow up request. So we can enable the KeepAlive Directive to restrict client to make different connection for every request.
So if you have a lots of traffic on your website then in some cases it will create a problen by enabling the KeepAlive. This is becuase we will have lots of unused open connection to our web server.
So how to overcome from this problem?
Here is the solution for this. We can set the [code]KeepAliveTimeout[/code] for the cache HTTP connection. So let’s see how to set the KeepAliveTimeout.
[cc lang=”apache”]
# Enaling the KeepAlive Connection
KeepAlive On
# But for just 2 secs
KeepAliveTimeout 2
[/cc]
Now webserver will cache that HTTP connection for just 2 secs so we will not have unrequired open connections.
Limit the number for Persistance Connections
Here we can set the how many number of requests to act as a Persistance Connection. [code]MaxKeepAliveRequests[/code] is used for this.
How to Set it?
[cc lang=”apache”]
MaxKeepAliveRequests 100
[/cc]
If this value is set to 0 then it will be considered as a unlimited numbers of requests.
Note: It is recommended to set the higher value of this Directive for higher performance.
Conclusion
It is recommended to enable the KeepAlive Directory but with some amount of KeepAliveTimeout.