OpenResty Edge load-balances TCP services at the stream layer entirely from its Admin console — no stream{} blocks, no upstream or proxy_pass directives, no nginx config editing or reloads. You create a TCP (stream) application, point it at an upstream of backend servers, and OpenResty Edge proxies the connections across its gateway network. In this tutorial we proxy a MySQL server on port 3307 and connect through an edge node to confirm it works. For the complete step-by-step walkthrough, watch the video tutorial on YouTube.

This is the same stream-layer proxying used for the SNI Proxy application type.

Diagram of OpenResty Edge reverse-proxying TCP connections to backend servers

Create a TCP (Stream) Application in OpenResty Edge

Let’s go to the OpenResty Edge Admin web console. It is our sample deployment of the console. Every user has their local deployment.

OpenResty Edge Admin web console home page

This time, we are going to create an application of type stream.

Let’s enter the stream applications page and create a new application.

Stream applications page in the OpenResty Edge Admin console

We see two types of stream applications: SNI Proxy and TCP.

Two stream application types offered in OpenResty Edge: SNI Proxy and TCP

Our topic today is TCP applications.

TCP stream application type highlighted in OpenResty Edge

SNI Proxy applications will get covered in another tutorial.

SNI Proxy stream application type in OpenResty Edge

Select the type TCP.

Selecting the TCP type for the new stream application

We need to specify a port for this application.

Port field for the new TCP application in OpenResty Edge

First, we need to add this port to the default partition.

Partition list where the TCP application port must be added

Click to edit the default partition, add a new port, and enter 3307 as the port number.

Entering port number 3307 for the TCP application

Set the port type to TCP.

Setting the port type to TCP in the partition editor

After saving, the new port is added to the default partition, and the change is released automatically across the gateway network.

TCP port 3307 added to the default partition in OpenResty Edge

Back in the new application dialog, enter the port we just added and create the application.

Entering port 3307 for the TCP application after adding it to the partition

Add an Upstream and Page Rule for TCP Load Balancing

Go to the upstream page.

Upstream page for the TCP application in OpenResty Edge

Create a new upstream for our backend server and give it a name, say, tcp_backend.

Naming the TCP upstream tcp_backend in OpenResty Edge

Here we need the backend server’s IP address.

IP address field for the backend server in the upstream editor

We’ve already prepared a MySQL server at this IP address. Connecting to it directly confirms it is accessible.

The backend MySQL server returns the select 1 result, confirming it is reachable directly

We can now fill out the host field for the backend server.

Filling in the backend MySQL server host in the TCP upstream

Enter port number 3307.

Entering backend port 3307 for the tcp_backend upstream

We may add more servers to this upstream in the future.

TCP upstream with room to add more backend servers for load balancing

Save this upstream. We can see the tcp_backend upstream is now listed.

tcp_backend upstream listed in OpenResty Edge

Now let’s create a new page rule to use this upstream.

Create new page rule button for the TCP application

Let’s add a proxy target here.

Adding a proxy target to the TCP page rule

Select the upstream that we just created.

Selecting the tcp_backend upstream as the proxy target

tcp_backend upstream set as the proxy target for the page rule

There are several balancing policies to choose from. Our upstream has only one server, so the balancing policy does not matter here — we just keep the default round-robin policy.

TCP load balancing policy options in the OpenResty Edge page rule, kept at round-robin

Create this page rule.

Create button for the TCP proxy page rule

We can see the newly created proxy page rule in the page rule list.

Newly created TCP proxy page rule in the page rule list

The last step is to make a new configuration release, which pushes our pending changes out to all the gateway servers.

New release button in the OpenResty Edge Admin console

Ship it.

Ship release button confirming the TCP configuration push

Now it is fully synchronized. As we can see, this sample deployment has 14 servers in the gateway network.

TCP configuration fully synchronized across 14 gateway servers

We do incremental config synchronization across the whole network, live-updating config on the request level. None of the application-level configuration changes require a server reload, restart, or binary upgrade, so it stays scalable even when many different users make frequent releases.

Request-level live config updates without any server reload or restart

Test the TCP Proxy: Connect to MySQL Through an Edge Node

Checking all the gateway servers grouped by clusters, we have a node in the United States with an IP address ending in 226. We will test the proxy that we just added using this node.

United States edge node with an IP address ending in 226

Now we connect a MySQL client to that edge node on port 3307 — using the node’s IP as the host — and run a select query.

Running a SELECT query against MySQL through the OpenResty Edge TCP proxy on port 3307

It works as expected! The select query returns through the edge node, so the MySQL backend is accessible just like accessing it directly.

The select 1 result returns through the OpenResty Edge TCP proxy, confirming the MySQL backend is reachable

This tutorial load-balances TCP connections within a single upstream, using the same console workflow as the HTTP reverse proxy and load balancer setup. To steer traffic across data centers or regions at the DNS level, see how global server load balancing works in OpenResty Edge.

Frequently Asked Questions

How do I load balance TCP without editing nginx config?

In OpenResty Edge you create a TCP (stream) application in the Admin console, add an upstream of backend servers, and attach a page rule that proxies to it — no stream{} blocks, upstream definitions, or proxy_pass directives are written by hand. Releasing the change pushes it to every gateway server with no reload, restart, or binary upgrade.

Can I proxy a MySQL database through OpenResty Edge?

Yes. In this tutorial the backend is a MySQL server listening on port 3307, and OpenResty Edge proxies TCP connections to it. After the release, connecting a MySQL client to the edge node on port 3307 reaches the database exactly as if you connected to the backend directly.

What is the difference between a TCP application and an SNI Proxy application?

Both are stream applications in OpenResty Edge. A TCP application proxies raw TCP connections on a given port, as shown here. An SNI Proxy application routes TLS traffic by the SNI hostname in the handshake; it is covered in the SNI Proxy application tutorial.

Can a TCP upstream have multiple backend servers?

Yes. The upstream in this tutorial has a single server, so the balancing policy does not matter, but you can add more servers and choose a balancing policy such as round-robin.

What is OpenResty Edge

OpenResty Edge is our all-in-one gateway software for microservices and distributed traffic architectures. It combines traffic management, private CDN construction, API gateway, security, and more to help you easily build, manage, and protect modern applications. OpenResty Edge delivers industry-leading performance and scalability to meet the demanding needs of high concurrency, high load scenarios. It supports scheduling containerized application traffic such as K8s and manages massive domains, making it easy to meet the needs of large websites and complex applications.

If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!

About The Author

Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..

Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.

OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.

As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.