4

I'm wondering whether it's possible to do this with Nginx:

server1.domain.com:9999 --> localhost:4000
server2.domain.com:9999 --> localhost:5000
server3.domain.com:9999 --> localhost:6000
server4.domain.com:9999 --> localhost:7000

With the services running on ports 4000, 5000, 6000 and 7000 not being HTTP software.

If it's not possible to use Nginx, what program can I use to make this possible (without it interfering with Nginx)? My VPS is running Ubuntu 18.04.

Thanks.

Sasino
  • 83

1 Answers1

6

Yes, you need to use streams.

In NGINX Plus Release 5 and later, NGINX Plus can proxy and load balance Transmission Control Protocol) (TCP) traffic. TCP is the protocol for many popular applications and services, such as LDAP, MySQL, and RTMP.

In NGINX Plus Release 9 and later, NGINX Plus can proxy and load balance UDP traffic. UDP (User Datagram Protocol) is the protocol for many popular non-transactional applications, such as DNS, syslog, and RADIUS.

Prerequisites

Latest NGINX Plus (no extra build steps required) or latest NGINX Open Source built with the --with-stream configuration flag

An application, database, or service that communicates over TCP or UDP

Upstream servers, each running the same instance of the application, database, or service

It's similar to a http block

worker_processes auto;

error_log /var/log/nginx/error.log info;

events { worker_connections 1024; }

stream { upstream backend { hash $remote_addr consistent;

    server backend1.example.com:12345 weight=5;
    server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3;
}

upstream dns {
   server 192.168.0.1:53535;
   server dns.example.com:53;
}

server {
    listen 12345;
    proxy_connect_timeout 1s;
    proxy_timeout 3s;
    proxy_pass backend;
}

server {
    listen 127.0.0.1:53 udp reuseport;
    proxy_timeout 20s;
    proxy_pass dns;
}

server {
    listen [::1]:12345;
    proxy_pass unix:/tmp/stream.socket;
}

}

https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/

HoD
  • 3,522