NGINX 透明 TCP 代理

Tan*_*DLJ 5 proxy tcp nginx

我有一个 ELK 堆栈。在两台 Logstash 主机前面,我设置了两个 NGINX 负载均衡器作为透明代理。UDP 流量正在发挥作用。TCP 与配置一起工作:

stream {
  upstream syslog {
    server sapvmlogstash01.sa.projectplace.com:514;
    server sapvmlogstash02.sa.projectplace.com:514;
  }
  server {
    listen 514;
    proxy_pass syslog;
  }
}
Run Code Online (Sandbox Code Playgroud)

但我得到的是 LB 的 source_ip 和 source_host 而不是输入服务器的 IP。

设置相同的添加proxy_bind $remote_addr transparent;不起作用,抛出超时。

*1 upstream timed out (110: Connection timed out) while connecting to upstream, client: $SOURCEHOST_IP, server: 0.0.0.0:514, upstream: "$LOGSTASH_IP:514", bytes from/to client:0/0, bytes from/to upstream:0/0
Run Code Online (Sandbox Code Playgroud)

我尝试从这里设置 TPROXY: https: //www.nginx.com/blog/ip-transparency-direct-server-return-nginx-plus-transparent-proxy/

Logstash 主机:

route add default gw $NGINX_IP
route del default gw $DEFAULT_GW
Run Code Online (Sandbox Code Playgroud)

NGINX 主机:

# Following nginx how-to
iptables -t mangle -N DIVERT
iptables -t mangle -A PREROUTING -p udp -m socket -j DIVERT
iptables -t mangle -A DIVERT -j MARK --set-xmark 0x1/0xffffffff
iptables -t mangle -A DIVERT -j ACCEPT
iptables -t mangle -A PREROUTING -p tcp -s $LOGSTASH_IP/24 --sport 514 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 0
ip rule add fwmark 1 lookup 100
ip route add local 0.0.0.0/0 dev lo table 100

# Enabling Upstream Servers to Reach External Servers
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)

但仍然像以前一样因超时而失败。

获得透明 TCP 主机还缺少什么?

小智 -4

官方文档说: proxy_bind $remote_addr 透明;

为了使该参数起作用,通常需要以超级用户权限运行 nginx 工作进程。在 Linux 上,不需要 (1.13.8),因为如果指定了透明参数,工作进程会从主进程继承 CAP_NET_RAW 功能。还需要配置内核路由表以拦截来自代理服务器的网络流量

仅供参考: https: //www.nginx.com/blog/ip-transparency-direct-server-return-nginx-plus-transparent-proxy/