NGINX keepalive connections: Explained with examples

Updated: January 21, 2024 By: Guest Contributor Post a comment

Introduction

When it comes to managing server connections, particularly for high-traffic websites, a solid understanding of keepalive connections is crucial. In this tutorial, we’ll dive deep into the world of NGINX and explore how to optimize keepalive connections along with some practical examples.

What does ‘Keepalive Connection’ mean?

Keepalive connections, also known as persistent connections, are a fundamental feature of the HTTP protocol that allows multiple requests to be sent over a single TCP connection without needing to reopen a new one for each request. This reuse of connections reduces the overhead of connection setup and reduces latency, leading to faster load times and improved resource utilization.

Configuring Keepalive in NGINX

To start configuring keepalive connections in NGINX, you’ll need access to your server’s NGINX configuration file, usually found at /etc/nginx/nginx.conf.

http {
    keepalive_timeout 65;
}

The above directive sets the keepalive_timeout, which defines how long a connection should remain open waiting for additional requests. By default, NGINX sets this value to 65 seconds, but you can adjust this number based on your specific needs.

Understanding the Keepalive Directives

NGINX offers several directives to manage keepalive connections:

  • keepalive_timeout: Sets the timeout for keepalive connections.
  • keepalive_requests: Limits the number of requests per connection. After this limit is reached, the server closes the connection.
  • keepalive_disable: Allows you to disable keepalive connections for specific user agents.

Examples of NGINX Keepalive Configuration

Simple Keepalive Setting

http {
    keepalive_timeout 30;
    keepalive_requests 100;
}

Here, we’ve reduced the timeout to 30 seconds and set the maximum number of requests per connection to 100. This means after 100 requests or 30 seconds of inactivity, the connection will close.

Disabling Keepalive for Old Browsers

http {
    keepalive_disable msie6;
}

In some cases, you might want to disable keepalive for certain clients, like outdated browsers which may not handle persistent connections well. The code above disables keepalive for Internet Explorer 6.

Optimizing Keepalive for Performance

While keepalive connections can significantly impact performance, they must be configured correctly to ensure that server resources are not exhausted by too many open connections. The following points should be considered:

  • Lowering the keepalive_timeout can free up resources, but it might not completely utilize TCP connection reusability.
  • Increasing keepalive_requests allows more data to be sent over the same connection but also holds the connection open longer.
  • Using the keepalive_disable selectively ensures that modern clients get the best performance while avoiding issues with clients that don’t manage keepalive well.

Combining these settings with an understanding of your traffic patterns, you can effectively optimize keepalive in NGINX.

Testing Keepalive Connections

Once you’ve made changes to your NGINX configuration, it’s important to test that keepalive is working correctly:

curl -Iv http://yourdomain.com

You should see the Connection: keep-alive header in the response if keepalive is enabled. Repeated requests with tools like curl can help you observe connection reuse.

Monitoring Keepalive Connections

Monitoring is vital to ensure your keepalive settings are suitably balanced for your application’s needs:

netstat -an | grep ':80.*ESTABLISHED' | wc -l

This command counts the currently established keepalive connections on port 80, helping you assess the real-time use of keepalive connections on your NGINX server.

When to Avoid Keepalive

While keepalive connections offer numerous benefits, there are scenarios when it’s best to avoid them:

  • When the server has minimal memory resources.
  • If the website serves an extremely high number of clients with very few repeat requests.

In such cases, the overhead of managing persistent connections can outweigh the benefits, and adjusting the keepalive_timeout lower or even disabling it may yield better performance.

Parting Words

In this tutorial, we’ve explored the intricacies of NGINX keepalive connections. Proper configuration and adjustment to your specific scenario can lead to significant performance gains. Apply these best practices, leverage examples, and test thoroughly to truly optimize your NGINX server’s performance.