NGINX.COM

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:

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
Load balancing (simple algorithms)
Connection retry on failure (passive monitoring)
SSL termination and encryption
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 ✅ (Least Time algorithm)
Health checks ✅ (HTTP and TCP)
Connection management ✅ (HTTP and TCP)
Session persistence ✅ (HTTP)
Dynamic reconfiguration ✅ (HTTP and TCP)
Live activity monitoring ✅ (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 to discuss your use cases.

Hero image
《NGINX 完全指南》2024 年最新完整版


高性能负载均衡的进阶使用指南

关于作者

Owen Garrett

产品管理高级总监

Owen is a senior member of the NGINX Product Management team, covering open source and commercial NGINX products. He holds a particular responsibility for microservices and Kubernetes‑centric solutions. He’s constantly amazed by the ingenuity of NGINX users and still learns of new ways to use NGINX with every discussion.

关于 F5 NGINX

F5, Inc. 是备受欢迎的开源软件 NGINX 背后的商业公司。我们为现代应用的开发和交付提供一整套技术。我们的联合解决方案弥合了 NetOps 和 DevOps 之间的横沟,提供从代码到用户的多云应用服务。访问 nginx-cn.net 了解更多相关信息。