SoFunction
Updated on 2025-03-03

Nginx Configuring TCP Proxy Forwarding Implementation

Use the new Nginx stream method to implement TCP/UDP proxy forwarding.

After Nginx installed the stream module.

Revise

Under the main configuration fileAdd stream configuration.

For example nginx home is/opt/software/nginx

cd /opt/software/nginx/conf

vim 

Added the following configuration

stream {

    log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

    access_log /opt/software/nginx/logs/ proxy ;
    open_log_file_cache off;

	# Unified placement for easy management	include tcpConf/*.conf;
}

Add stream configuration

Create a tcp configuration folder to facilitate the unified management of configuration files in the future:

cd /opt/software/nginx/conf

mkdir tcpConf

Create a configuration file

cd tcpConf

vim 

Edited as follows:

upstream tcp9004 {
    server 118.178.188.188:8992;
}

server {
    listen 9004;
    proxy_connect_timeout 8s;
    proxy_timeout 24h;
    proxy_pass tcp9004;
}

Test to restart Nginx

Test Nginx

cd /opt/software/nginx

./sbin/nginx -t

After no problem

Restart Nginx

./sbin/nginx -s reload

Port monitoring

Use tcpdump to listen for tcp data

Install tcpdump

yum install -y tcpdump

View network card

ifconfig

Listen to the corresponding port on the network card

tcpdump -n -v -i eth1 port 8992

This is the end of this article about Nginx's configuration of TCP proxy forwarding. For more information about Nginx TCP proxy forwarding, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!