SoFunction
Updated on 2025-03-10

Sample code for nginx to implement TCP reverse proxy

Current experimental environment:
nginx installed version 1.11.13
Need to dynamically expand the installation module nginx_tcp_proxy_module to implement tcp reverse proxy

Experimental steps:

1. The current version of nginx 1.11.13 (nginx has been installed)

# /alidata/nginx/sbin/nginx -v
nginx version: nginx/1.13.7

2. Check the previous installation module

# /alidata/nginx/sbin/nginx -V
nginx version: nginx/1.13.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/alidata/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/src/pcre-8.12/ --add-module=/soft/soft/ngx_http_substitutions_filter_module/ --add-module=/soft/soft/ngx_http_google_filter_module

3. Enter nginx source code installation package

# cd /usr/src/nginx-1.13.7/
# ls
auto       configure  html     Makefile  objs              README
CHANGES  conf        contrib    LICENSE  man       pcre-8.  src

4. Dynamically add tcp reverse proxy module – with-stream=dynamic (This module is the same as the third-party module nginx_tcp_proxy_module-master)

# ./configure  --prefix=/alidata/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/src/pcre-8.12/ --add-module=/soft/soft/ngx_http_substitutions_filter_module/ --add-module=/soft/soft/ngx_http_google_filter_module --with-stream=dynamic
# make # Only compile here, don't make install

5. Check the current directory's objs/ directory and a .so file [ngx_stream_module.so] will be generated.

# ls objs/
addon         nginx              ngx_auto_headers.h  ngx_stream_module_modules.c  src
  nginx.8            ngx_modules.c       ngx_stream_module_modules.o
Makefile      ngx_auto_config.h  ngx_modules.o       ngx_stream_module.so

6. Create a modules directory in the nginx installation directory and move this .so file to the modules directory

# cd /alidata/nginx/
# mkdir modules #If it exists, there is no need to create it# chown  modules
# cp /usr/src/nginx-1.13.7/objs/ngx_stream_module.so  /alidata/nginx/modules/

7. Load the module into the main configuration file of nginx and add the configuration of tcp

# vi /alidata/nginx/conf/
load_module modules/ngx_stream_module.so;     #Loading module
events {
    ......
}
#-------------------- HTTP -------------------------------
http {
    ......
}


#tcp and http are of the same level, here only tcp reverse proxy configuration#-------------------- TCP/UDP -------------------------------
#include /alidata/nginx//*.conf; #You can also point the tcp configuration to a separate configuration file

stream {                                   #stream is a fixed writing method, which means that this is the tcp protocol. If it is installed through the third-party module [nginx_tcp_proxy_module], it will be replaced with tcp.
    upstream proxy_swoole {
        server 172.16.100.17:8090;
    }

    server {
       listen 10001;            #Access http://ip:10001 and jump to 172.16.100.17:8089       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass proxy_swoole;
    }
}

Explain the difference between tcp and stream:

tcp {                                    #tcp means traditional installation of nginx_tcp_proxy_module through third-party modules
        upstream cluster {
                server localhost:2000;
                server localhost:3000;
        }
        server{
                listen 8080;
                proxy_pass cluster;
        }
}

--------------------------------------------------------------------------
stream {                                  #stream means dynamic installation through third-party modules --with-stream=dynamic        upstream cluster {
                server localhost:2000;
                server localhost:3000;
        }
        server{
                listen 8080;
                proxy_pass cluster;
        }
}

8. Reload nginx service

# /alidata/nginx/sbin/nginx -s reload
# netstat -npult|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      34716/nginx         
tcp        0      0 0.0.0.0:10001               0.0.0.0:*                   LISTEN      34716/nginx         
tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      34716/nginx  

9. Visit http://172.16.12.9:10001, and the instructions are successfully redirected

This is the article about nginx's example code to implement TCP reverse proxy. For more related nginx TCP reverse proxy content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!