The keepalive directive in Apache is used to keeps the HTTP connection open instead of opening a new connection with each and every new request. This is also known as persistence connection which is the feature of HTTP/1.1 for long-lived HTTP connection.Here you will get the exact idea how to configure keepalive directive in apache.
Let’s see the syntax of KeepAlive directive:
SYNTAX:
1 2 3 | KeepAlive On|Off |
KeepAlive specifies whether persistent connections should be On or Off. Default value for KeepAlive is On so it is enabled by default and make connections keep open.You can disable persistent connection setting as Off.
Note: A KeepAlive connection with HTTP/1.0 can only be used if length of the content is known in advance.
Basically, KeepAlive directive keeps each HTTP connection open to avoid unnecessary delay. This is useful when client tries to make a several connection to the web server so the KeepAlive Directive allows client to make the different connection for every request. When you have lots of traffic on your website, this feature will create a problem in some cases because web server will have lots of unused open connection.
To Solve this issue when you have more traffic.You can set the KeepAliveTimeout to cache HTTP connection and to avoid waiting too long for the next request.
Let’s understand about the KeepAliveTimeout.
SYNTAX:
1 2 3 | KeepAliveTimeout seconds |
KeepAliveTimeout directive sets the number of seconds to wait for the next request and once the request has been received, the TimeOut directive applies.
TimeOut directive in Apache
Here is the syntax for TimeOut directive.
SYNTAX:
1 2 3 | TimeOut seconds |
Default seconds for TimeOut is 1200.You can set the maximum time so that the server will wait for the receipt of a request. TimeOut directive useful to downloads of large files over slow connections.
Here is the line of code you can set for KeepAlive and KeepAliveTimeout directive.
1 2 3 4 5 6 | # Enable the KeepAlive Connection KeepAlive On # For specific 5 secs KeepAliveTimeout 5 |
As per above code, the web server will cache that HTTP connection for the specific time which is here 5 seconds and it will not open unused and unrequired connections.
To Limit the number of connections
MaxKeepAliveRequests directive is used for a maximum number of KeepAlive requests per connection and it will have 0 for infinite.Let’s set the number of requests to perform a Persistent Connection.
SYNTAX:
1 2 3 | MaxKeepAliveRequests number |
Let’s see the example of MaxKeepAliveRequests directive:
1 2 3 | MaxKeepAliveRequests 500 |
If this value is set to 0 then it will be considered as infinite requests.
Note: I recommend to set the higher value of this Directive for maximum server performance.
That’s it with KeepAlive directive.It is good to enable the KeepAlive Directive with KeepAliveTimeout.