Methods of payment Abuse

How to Set HTTP Request Timeouts in Linux: A Complete Guide

17.02.2025, 19:30

When working with HTTP requests in Linux, it’s crucial to configure timeouts correctly to avoid system hangs, indefinite waiting for server responses, and resource overloads. This is especially critical when dealing with web services, APIs, automated scripts, and downloads, where unstable connections or overloaded servers can cause significant delays. In this guide, we’ll cover in detail how to set HTTP request timeouts using various tools, from command-line utilities to web server configurations and system parameters.

What Is an HTTP Request Timeout and Why Is It Important?

An HTTP request timeout is a time limit within which a client (browser, server application, or script) waits for a response from the server. If the server doesn’t respond within the set time, the connection is forcibly terminated, and the client receives an error message.

Main Causes of Timeouts:
→ Server overload – The server is handling too many requests simultaneously.
→ Network issues – Weak or broken internet signals, packet loss, or high latency.
→ Client-side problems – Slow data processing or insufficient resources on the device.
→ Connection hanging – The server keeps the connection open but does not respond.

Why Limit HTTP Request Waiting Time?
→ Reduces server load – Long connections don’t consume resources indefinitely.
 → Improves system performance – Resources are distributed more efficiently.
 → Prevents hangs – The system doesn’t wait for a response forever.
 → Enhances security – Protects against DoS (Denial-of-Service) attacks.

Now, let’s explore how to configure HTTP request timeouts using different tools and settings.

1. Using curl

What is it?

curl is a powerful command-line utility for making HTTP requests. It supports multiple parameters, including timeout settings.

How to Set a Timeout?

To set a limit on the maximum response waiting time, use the --max-time parameter:

curl --max-time 10 https://example.com

In this example, curl waits for a response for no more than 10 seconds. If the server does not respond within this time, the connection is terminated,

and an error is returned.
You can also set a connection timeout (--connect-timeout):
curl --connect-timeout 5 --max-time 15 https://example.com

Here, curl:
→ Waits up to 5 seconds to establish a connection.
→ Allows a maximum request execution time of 15 seconds.

2. Configuring Timeout in Apache

Where to Set It?

Apache configuration is edited in httpd.conf or apache2.conf (depending on the Linux distribution).

Key Parameters:
→ Timeout – Sets the overall request timeout.
→ ProxyTimeout – Used when Apache functions as a reverse proxy.

Example Configuration:

Timeout 60
ProxyTimeout 30

In this example:
→ Apache closes the connection if a request takes longer than 60 seconds.
→ If Apache acts as a proxy, it will wait up to 30 seconds for a response from the backend.

After making changes, restart the server:

systemctl restart apache2

3. Configuring Timeout in Nginx

Where to Edit?

The configuration file is nginx.conf, typically located in /etc/nginx/nginx.conf.

Key Parameters:
→ client_header_timeout – Timeout for receiving request headers.
→ client_body_timeout – Timeout for receiving the request body.
→ send_timeout – Timeout for sending a response to the client.
→ proxy_read_timeout – Timeout for reading a response from a proxy server.

Example Configuration:

http {
    client_header_timeout 10s;
    client_body_timeout 10s;
    send_timeout 10s;
    proxy_read_timeout 30s;
}

These settings:
→ Terminate client requests if there is no response within 10 seconds.
→ Wait up to 30 seconds for a backend response.

To apply the settings, restart Nginx:
systemctl restart nginx

4. Using wget

What is it?

wget is a command-line tool for downloading files over HTTP.

How to Set a Timeout?

wget --timeout=15 https://example.com/file.zip

This sets a 15-second timeout, after which the connection is terminated.

5. Configuring Timeout in PHP

When Is This Needed?

When making HTTP requests in PHP, it’s essential to limit script execution time to prevent hangs.

How to Configure?

In php.ini or directly in the code:

ini_set('max_execution_time', 30); // Limit to 30 seconds

This prevents PHP scripts from hanging due to long HTTP requests.

6. Configuring System-Level Timeouts

When Is This Needed?

If you need to control network timeouts globally for the entire system.

How to Configure?

Edit /etc/sysctl.conf and add:

net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 600

Apply the changes:

sysctl -p

These parameters control:
→ Closing connections after 30 seconds (tcp_fin_timeout).
→ Keepalive checks every 10 minutes (tcp_keepalive_time).

Additional Tips for HTTP Timeout Configuration

Choose Optimal Timeout Values

Setting timeouts too short may cause unnecessary errors due to minor network delays. However, excessively long timeouts can overload the server.
Recommended timeout values (adjustable for your infrastructure):
→ curl / wget / HTTP clients: 10–30 seconds, depending on request priority.
→ Apache / Nginx: 30–60 seconds for client connections, 15–30 seconds for proxies.
→ PHP / backend scripts: 30 seconds (longer for complex tasks).
→ Linux system settings: 30 seconds for closing connections, 10 minutes for keepalive checks.

If you run an API server, consider different timeouts for GET and POST requests:
→ GET requests (fetching data) – 5–10 seconds.
→ POST requests (modifying data) – 20–30 seconds.

Use Retry Mechanisms

If a request fails due to a timeout, it may be useful to retry it instead of immediately returning an error.

How to do this? In curl, use --retry:

curl --max-time 10 --retry 3 --retry-delay 5 https://example.com

This means 3 retry attempts with 5-second intervals between them.

Monitor Logs and Analyze Timeout Errors

If timeouts occur frequently, investigate whether the issue is with the server, network, or client.

Where to find logs?
→ Apache: /var/log/apache2/error.log
→ Nginx: /var/log/nginx/error.log
→ PHP: /var/log/php_errors.log
→ System timeouts: dmesg | grep timeout or journalctl -xe

If timeouts happen too often, check:
→ CPU load (top, htop).
→ Network status (netstat -an, ss -tulnp).
→ Server resource usage (free -m, iostat).

Conclusion

Setting HTTP request timeouts is a key aspect of optimizing servers and network applications.

→ curl and wget – Allow setting HTTP request time limits.
→ Apache and Nginx – Help manage web server timeouts.
→ PHP – Prevents script hangs during long HTTP requests.
→В Linux system settings – Provide global network connection control.

Choosing the right method depends on your infrastructure, but proper timeout settings will help you avoid hangs, improve performance, and protect servers from overloads.