html
KeepAlive is an HTTP feature that allows multiple requests to be sent over a single TCP connection, reducing latency and improving page load times. By default, Apache2 disables this feature (KeepAlive Off
) due to potential resource consumption concerns.
The main directives controlling KeepAlive behavior in Apache2 are:
# In httpd.conf or apache2.conf
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
Consider enabling KeepAlive when:
- Your server hosts many small static assets (CSS, JS, images)
- You observe high latency between client and server
- Your traffic pattern involves multiple sequential requests
Use ab
(Apache Benchmark) to test both configurations:
# Without KeepAlive
ab -n 1000 -c 100 http://yoursite.com/
# With KeepAlive
ab -n 1000 -c 100 -k http://yoursite.com/
For a media-heavy site:
<IfModule mod_http2.c>
KeepAlive On
KeepAliveTimeout 2
MaxKeepAliveRequests 500
</IfModule>
For an API endpoint:
<VirtualHost *:443>
KeepAlive Off
# API responses are typically single requests
</VirtualHost>
After enabling KeepAlive, monitor these metrics:
apachectl status | grep 'KeepAlive'
netstat -anp | grep httpd | grep ESTABLISHED | wc -l
If you encounter problems:
- Check error logs:
tail -f /var/log/apache2/error.log
- Verify module is loaded:
apache2ctl -M | grep keepalive
- Adjust timeout values incrementally
KeepAlive is an HTTP feature that allows multiple requests to be sent over a single TCP connection. In Apache2, this setting is controlled by three main directives:
KeepAlive On|Off KeepAliveTimeout [seconds] MaxKeepAliveRequests [number]
The decision to enable KeepAlive depends on your server's workload characteristics:
- High-latency connections: KeepAlive significantly benefits mobile clients or geographically distributed users
- Resource-constrained servers: May suffer from connection exhaustion with KeepAlive enabled
- Static-heavy sites: Typically see greater benefits than dynamic content sites
For a general-purpose web server handling mixed content:
# /etc/apache2/apache2.conf or .htaccess KeepAlive On KeepAliveTimeout 2 MaxKeepAliveRequests 100
For a high-traffic API server:
KeepAlive On KeepAliveTimeout 5 MaxKeepAliveRequests 0 # Unlimited requests per connection
Use tools like ab
(Apache Benchmark) to test different configurations:
ab -n 1000 -c 100 -k http://yourserver.com/test.html
Key metrics to monitor:
- Requests per second
- Time per request
- Connection times
If you experience connection problems after enabling KeepAlive:
# Check current connections netstat -ant | grep :80 | wc -l # Monitor server status apache2ctl status