How to Adjust KeepAliveTimeout in Apache?
This article provides a step-by-step guide on how to configure the
KeepAliveTimeout directive in the Apache HTTP Server. You
will learn what this setting does, why it is important for server
performance, where to locate the configuration files on different
operating systems, and how to safely apply your changes. By optimizing
this value, you can strike the right balance between server resource
consumption and a fast user experience.
Understanding KeepAliveTimeout
The KeepAliveTimeout directive defines the number of
seconds Apache will wait for a subsequent request on a persistent
connection before closing it. When a browser connects to your server, it
often needs to download multiple files like HTML, CSS, JavaScript, and
images. Enabling KeepAlive allows these files to be sent over a single
connection rather than opening a new one for every file.
- Too High: If the timeout is set too high (e.g., 15 seconds or more), idle connections will stay open, tying up server RAM and worker processes that could be serving other active users.
- Too Low: If it is set too low (e.g., 1 second), the connection might close before the browser can request the next asset, forcing it to establish a new connection and slowing down page load times.
- The Sweet Spot: The default setting is typically 5 seconds, but many high-traffic sites reduce this to 2 to 3 seconds to free up resources quickly without sacrificing performance.
Step-by-Step Configuration
To modify this setting, you need administrative (root) access to your server via SSH.
1. Locate the Configuration File
Depending on your operating system, the main Apache configuration file or the specific KeepAlive configuration file will be in different locations:
- Ubuntu / Debian:
/etc/apache2/apache2.conf(or sometimes inside/etc/apache2/mods-enabled/mpm_prefork.confor similar, though global settings are usually inapache2.conf). - CentOS / RHEL / Fedora:
/etc/httpd/conf/httpd.conf
2. Edit the File
Open the configuration file using a text editor like
nano or vi. For example, on Ubuntu, run:
sudo nano /etc/apache2/apache2.conf3. Modify the Directives
Scroll through the file to find the KeepAlive settings. If they do
not exist, you can add them to the bottom of the file. Ensure
KeepAlive is turned on, and then adjust the timeout
value:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 3In this example, Apache is configured to wait 3 seconds for a new request before dropping the connection.
4. Test the Configuration
Before restarting Apache, always test your configuration file for syntax errors to avoid bringing down your website.
- On Ubuntu/Debian:
sudo apache2ctl configtest - On CentOS/RHEL:
sudo httpd -t
If the terminal outputs Syntax OK, you are safe to
proceed.
5. Restart Apache
Apply the changes by restarting the Apache service:
- Ubuntu / Debian:
sudo systemctl restart apache2- CentOS / RHEL:
sudo systemctl restart httpdVerifying the Changes
You can verify that your settings are working by monitoring your
server traffic or using developer tools in your web browser. Inspect the
network tab while loading your website; the
Connection: keep-alive header should be visible in the
response headers, and idle persistent connections should close
automatically according to your newly defined limit.