Example of using Nginx to implement port forwarding TCP proxy

Example of using Nginx to implement port forwarding TCP proxy

Demand Background

Recently, a colleague deployed an application in the test and production environments respectively. Since the application can only integrate LDAP, and our company uses AD, I built an OpenLDAP service. The account is first synchronized from AD to OpenLDAP through lsc, and then saslauthd is used to pass the authentication to AD. In the test environment, our application can connect to LDAP to log in, but in the production environment, it cannot access the OpenLDAP server. I don't want to repeatedly reinstall and maintain a set of OpenLDAP services in the production environment. This process is cumbersome and requires a scheduled task to synchronize the AD account with OpenLDAP every day. So I think this can be achieved through port forwarding. The topology between the nodes is roughly like the following.

insert image description here

Why use Nginx?

Search for port forwarding on Baidu. There are many ways to implement it. Most of them are implemented through Iptables. However, I tested it on Ubuntu and CentOS servers and it didn't work. I had no choice but to choose Nginx to implement it.

Port forwarding with Nginx

If our operating system is relatively new, we can directly install nginx through the software source. As long as the nginx version is greater than 1.9, it supports TCP proxy by default.

Check whether nginx supports TCP proxy

nginx -V

When we include --with-stream in the output configuration parameters, it means that nginx supports TCP proxy.

Install Dependencies

RHEL/CentOS/Fedora

yum install -y pcre* openssl*

Debian/Ubuntu

apt-get install zlib1g-dev libpcre++-dev openssl

Download dependencies

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar -zxvf ngx_cache_purge-2.3.tar.gz -C /usr/local/src

Download and install

Download source package

wget http://nginx.org/download/nginx-1.9.9.tar.gz

Unzip

tar -zxf nginx-1.9.9.tar.gz

Compile and install

cd nginx-1.9.9
./configure --prefix=/usr/local/nginx \
  --add-module=/usr/local/src/ngx_cache_purge-2.3 \
  --with-http_stub_status_module --with-stream
make && make install;

Modify the configuration file

/usr/local/nginx/conf/nginx.conf

events {
  ...
}

stream {
    upstream ldap {
        hash $remote_addr consistent;
        server 192.168.1.8:389;
    }
    server {
        listen 1389;
        proxy_connect_timeout 5s;
        proxy_timeout 5s;
        proxy_pass ldap;
    }
}

http {
  ...
}

In this example, we forward the local port 1389 to port 389 on 192.168.1.8

Start and check whether the service is normal

Start nginx service

/usr/local/nginx/sbin/nginx

Check the nginx process

netstat -anput | grep nginx

This concludes this article about using Nginx to implement port forwarding TCP proxy implementation examples. For more related Nginx port forwarding TCP proxy content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx stream configuration proxy (Nginx TCP/UDP load balancing)
  • nginx builds tcp proxy server

<<:  Example code for css3 to achieve scroll bar beautification effect

>>:  MySQL cursor detailed introduction

Recommend

Native js to implement form validation function

Table of contents When developing, analyzing the ...

Detailed explanation of the difference between tinyint and int in MySQL

Question: What is the difference between int(1) a...

JavaScript anti-shake and throttling explained

Table of contents Stabilization Throttling Summar...

How to turn off eslint detection in vue (multiple methods)

Table of contents 1. Problem Description 2. Probl...

Implementation example of Nginx+Tomcat load balancing cluster

Table of contents introduction 1. Case Overview 2...

MySQL query specifies that the field is not a number and comma sql

Core SQL statements MySQL query statement that do...

Guide to using env in vue cli

Table of contents Preface Introduction-Official E...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

How does MySQL achieve multi-version concurrency?

Table of contents MySQL multi-version concurrency...

Tutorial on how to connect and use MySQL 8.0 in IDEA's Maven project

First, let's take a look at my basic developm...

Summary of Linux date command knowledge points

Usage: date [options]... [+format] or: date [-u|-...

Detailed explanation of the use of React list bar and shopping cart components

This article example shares the specific code of ...