TCP Load Balancing with NGINX 1.9.0 and NGINX Plus R6

TCP load balancing is a powerful new feature in NGINX and NGINX Plus. It first debuted in NGINX Plus Release 5, and we’ve extended the feature with the recent release of NGINX Plus Release 6.

Now, in NGINX 1.9.0 we’ve added the core of the TCP load balancing feature to the open source product. We’ve devoted a great many person‑months to development and testing work, and it’s a good illustration of how development for the commercial product can support the open source product.

Editor –

Why Would a Web Server Need TCP Load Balancing?

NGINX is much more than a web server. It began life as a reverse proxy for HTTP traffic to accelerate other web servers. Over time it gained more and more features that offloaded work from the web servers it was accelerating, and NGINX is now a very complete and efficient open source combination of web server and caching/accelerating reverse proxy and application gateway.

NGINX is commonly used as the front end for busy websites – 44.9% of the 10,000 busiest sites use NGINX as their front end as of this writing. If NGINX is managing web traffic, it’s logical for it to act as a front end for other application traffic types as well, and that’s what the TCP load balancing feature provides for.

TCP load balancing can also benefit you when you use NGINX or NGINX Plus internally to proxy and load balance an application. Modern applications use a range of protocols, not all of which are HTTP-based. NGINX and NGINX Plus can be used to handle all of the internal traffic – MySQL load balancing is a good example.

How Do I Configure TCP Load Balancing?

TCP load balancing is configured in the stream configuration context. This context, like the http and mail contexts, enables you to configure one or more listen servers for TCP connections. Connections are forwarded to the upstream server group named by the proxy_pass directive. A load‑balancing algorithm is used to select one of the upstream servers (in this example, db1, db2, or db3):

stream {
    server {
        listen 3306;
        proxy_pass db;
    }

    upstream db {
        server db1:3306;
        server db2:3306;
        server db3:3306;
    }
}

This simple configuration employs the features in NGINX Plus R5, so the TCP Load Balancing in NGINX Plus R5 blog post is a good introduction. To learn how to configure all the R6 features, see TCP Load Balancing in the NGINX Plus Admin Guide and the reference documentation for the four stream modules:

What Does the Future Hold for TCP Load Balancing in NGINX and NGINX Plus?

Our plan for TCP load‑balancing features in NGINX and NGINX Plus follows the pattern we’ve established for HTTP proxying and load balancing:

nginx load balancing

Open source NGINX covers reverse proxying (accepting and forwarding traffic to an upstream server) and load balancing (choosing from a group of upstream servers based on a selection algorithm). If you’re prepared to build and support your own NGINX instances, you can pull in various third‑party modifications that implement advanced features not included in the standard NGINX distribution.

NGINX Plus covers reverse proxying and load balancing, and adds the features you see listed in the table for commercial application delivery controller (ADC) products, such as health checks, connection management, session persistence, remote configuration, and monitoring:

NGINX 1.9.0 NGINX Plus
Core features
Reverse proxy for HTTP and TCP Yes Yes
Load balancing (simple algorithms) Yes Yes
Connection retry on failure (passive monitoring) Yes Yes
SSL termination and encryption Yes Yes
Advanced ADC features
Some features might be available in third‑party modules All features are provided and
supported by NGINX, Inc.
Advanced load balancing algorithms Yes (Least Time algorithm)
Health checks Yes (HTTP and TCP)
Connection management Yes (HTTP and TCP)
Session persistence Yes (HTTP)
On‑the‑fly reconfiguration Yes (HTTP and TCP)
Live activity monitoring Yes (HTTP and TCP)

As we develop features in NGINX and NGINX Plus for HTTP load balancing, we’ll seek to develop equivalent features for TCP where possible, resulting in a consistent and predictable roadmap for both.

Editor –

To try out TCP load balancing with NGINX Plus for yourself, start your free 30‑day trial today or contact us for a live demo

NGINX recognized by Gartner in 2016 Magic Quadrant for Application Delivery Controllers

Everything you need to know about choosing the right ADC