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/