nginx1.9+ do TCP proxy (port forwarding)

Source: Internet
Author: User
Tags nginx server

As the title shows, Nginx can act as a port forwarding after version 1.9, that is, to access the specified port of the server, Nginx can act as a port forward to redirect traffic to another server, obtain the return data of the target server, and return it to the requester. Nginx's TCP proxy function differs from the reverse proxy of Nginx: All traffic that requests the port is forwarded to the target server, while in the reverse proxy it is possible to refine which requests are distributed to which servers, and the other is that Nginx does not simply restrict the URL request to the Web. You can also forward point-to-point requests such as memcached, MySQL, etc.

The implementation steps are as follows:

(1) nginx add "–with-stream" at compile time:

./configure–prefix=/usr/local/nginx–user=www–group=www–with-http_stub_status_module–with-pcre=/usr/local/src/ Pcre-8.38–add-module=/usr/local/src/ngx_cache_purge-2.3–with-http_gzip_static_module–with-stream


Where/usr/local/src/ngx_cache_purge-2.3 is the download ngx_cache_purge-2.3 unpacked directory

/usr/local/src/pcre-8.38 is the download pcre-8.38 unpacked directory


(2) Modify Nginx configuration file nginx.conf:

[Root@tkde-iphone ~]# vim/usr/local/nginx/conf/nginx.conf
user  www www;

worker_processes  32;  pid        logs/nginx.pid; events {     #use  epoll;                            
  #Linux最常用支持大并发的事件触发机制     worker_connections  65535; } stream {    upstream zifangsky {       
 hash  $remote _addr consistent;
        server 10.10.100.31:8000;    &NBSP}     server {        
listen 8080;
        proxy_connect_timeout 5s;
        proxy_timeout 5s;         proxy_pass zifangsky;    &NBSP}} http {    include       
Mime.types;

    default_type  application/octet-stream;
    sendfile        on;

     #tcp_nopush      on;
     #keepalive_timeout   0;

    keepalive_timeout  65;

     #gzip   on;     server {        listen   
    9000;

        server_name  localhost;

         #charset  koi8-r;

         #access_log   logs/host.access.log  main;         location / {            root 
  html;             index  index.html 
index.htm;         }    &nbsp}}

In the above configuration file, when you access the 8080 port of this server, the traffic is forwarded to the 10.10.100.31 server on port 8000.

(3) Check to see if the port is listening:

[Root@app01 nginx]# NETSTAT-APN | grep 8080:

(4) Test connection target Port:

[Root@app01 nginx]# telnet 10.10.100.31 8000
trying 10.10.100.31 ...
Connected to 10.10.100.31.
Escape character is ' ^] '.

(5) test the 8080 port port on the Nginx server on the other client:

[root@app05 ~]# telnet 192.168.1.30 8080
trying 192.168.1.30 ...
Connected to 192.168.1.30.
Escape character is ' ^] '.
Connection closed by foreign host.

Of course, the back is on the client side of the original connection to the 10.10.100.31 to connect to the Nginx server address, if the business does not have problems, it is configured to complete the

This article is from the "Zifangsky Personal blog" blog, please be sure to keep this source http://983836259.blog.51cto.com/7311475/1864242

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.