WARNING

How to Fix Apache 408 Request Timeout

Quick Fix Summary

TL;DR

Increase the `TimeOut` directive in your Apache configuration and verify client-side network stability.

Apache returns a 408 status code when a client's request takes longer to transmit than the server's configured timeout period. This is a server-side protection mechanism, not necessarily a client error.

Diagnosis & Causes

  • Slow client network connection or packet loss.
  • Insufficient `TimeOut` directive value in Apache config.
  • Heavy server load causing request processing delays.
  • Misconfigured `KeepAliveTimeout` for persistent connections.
  • Proxy or Load Balancer timeouts being shorter than Apache's.
  • Recovery Steps

    1

    Step 1: Increase Core Apache Timeout Directives

    The primary fix is to adjust the `TimeOut` directive, which governs the total time Apache waits for I/O (receiving request, sending response). Also, adjust `KeepAliveTimeout` if using persistent connections.

    bash
    # Edit your main Apache config (e.g., httpd.conf, apache2.conf) or a virtual host file.
    # Increase the global timeout (default is 60 seconds).
    TimeOut 300
    # Adjust KeepAliveTimeout if needed (default 5 seconds).
    KeepAliveTimeout 15
    # After editing, test the configuration syntax.
    sudo apachectl configtest
    # If syntax is OK, restart Apache.
    sudo systemctl restart apache2  # For systemd
    # OR
    sudo service apache2 restart     # For SysVinit
    2

    Step 2: Investigate and Optimize Slow Application Backend

    A 408 often masks a slow application (e.g., PHP, Node.js). The client is waiting for the app, but Apache cuts the connection. Profile your application and increase backend timeouts.

    bash
    # Example: For PHP-FPM, increase `request_terminate_timeout` in www.conf.
    request_terminate_timeout = 300s
    # For mod_proxy to a backend (e.g., Tomcat, another Apache).
    ProxyTimeout 300
    # Place this in your virtual host or proxy configuration section.
    # Use Apache's mod_status to identify slow requests (requires configuration).
    # Enable mod_status and visit /server-status to see 'Seconds since request began'.
    3

    Step 3: Analyze Logs and Client-Side Issues

    Use Apache logs to identify patterns. A surge in 408s from specific IPs or during specific times points to network or client issues, not just server config.

    bash
    # Tail the Apache error log for 408 entries.
    sudo tail -f /var/log/apache2/error.log | grep "408"
    # Check the access log for corresponding client IPs and URLs.
    sudo tail -f /var/log/apache2/access.log | grep "408"
    # Use netstat/ss to check for many connections in FIN_WAIT or TIME_WAIT states from problematic clients.
    sudo ss -tan | grep 'TIME-WAIT' | head -20
    4

    Step 4: Tune OS and Network Parameters (Advanced)

    In extreme cases, especially under high concurrency, you may need to adjust TCP stack parameters to allow the OS to handle more pending connections gracefully.

    bash
    # Increase the maximum number of pending connections (somaxconn).
    echo 'net.core.somaxconn = 1024' | sudo tee -a /etc/sysctl.conf
    # Increase TCP buffer sizes for high-latency networks.
    echo 'net.ipv4.tcp_rmem = 4096 87380 16777216' | sudo tee -a /etc/sysctl.conf
    echo 'net.ipv4.tcp_wmem = 4096 65536 16777216' | sudo tee -a /etc/sysctl.conf
    # Apply the changes.
    sudo sysctl -p

    Architect's Pro Tip

    "For AJAX-heavy applications, set a longer `TimeOut` specifically in the relevant `<Location>` or `<Proxy>` block, rather than globally, to avoid resource exhaustion from idle connections."

    Frequently Asked Questions

    Is Apache 408 a client or server error?

    Officially a 'Client Error' (4xx), the 408 is triggered by the server's configuration. The root cause can be a slow client, a slow server/application, or an incorrectly low timeout value on the server.

    What's the difference between TimeOut and KeepAliveTimeout?

    `TimeOut` is the total time Apache waits for any I/O event (receiving headers, writing response). `KeepAliveTimeout` is how long Apache waits for a subsequent request on a persistent (KeepAlive) connection before closing it.

    Can increasing TimeOut cause server performance issues?

    Yes. Setting `TimeOut` too high on a busy server can lead to worker threads/processes being tied up waiting for slow clients, potentially exhausting available connections (MaxClients). Always monitor connection states after changes.

    Related Apache Guides