search

Home  >  Q&A  >  body text

socket - Issues with Nginx TCP proxy

The device uses port 1883 to forward to the backend server through nginx's TCP/UDP proxy.
The backend server checks the device's socket IP and it is the IP of the proxy server, not the actual IP of the device.
How to implement transparent proxy ,Let the nginx agent connect the actual IP of the device to the backend server?

Remark:

  1. The device cannot get its own IP when connecting, so don’t think about it

  2. is a TCP/UDP proxy, not http

阿神阿神2750 days ago661

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-05-16 17:14:03

    Nginx Stream Module
    The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.

    This module is not included in the default build. You need to add --with-streamconfiguration when installing nginx.

    Example program

    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;
            proxy_responses 1;
            proxy_timeout 20s;
            proxy_pass dns;
        }
    
        server {
            listen [::1]:12345;
            proxy_pass unix:/tmp/stream.socket;
        }
    }

    reply
    0
  • Cancelreply