Nginx TCP forwarding based on hostname

前端 未结 3 895
孤独总比滥情好
孤独总比滥情好 2020-11-27 10:31

With the release of TCP load balancing for the Nginx community version, I would like to mix OpenVPN and SSL pass-through data. The only way for Nginx to know how to route th

3条回答
  •  盖世英雄少女心
    2020-11-27 11:10

    AS @Lochnair mentioned, you can use ngx_stream_map module and variable $server_addr to resolve this problem. Here is my example.

    My host IP is 192.168.168.22, and I use keepalived bound 2 virtual IP to eth0.

    $sudo ip a
    ...
    2: eth0:  mtu 1500 qdisc mq state UP qlen 1000
    link/ether 5c:f3:fc:b9:f0:84 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.22/24 brd 192.168.168.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.168.238/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.168.239/32 scope global eth0
       valid_lft forever preferred_lft forever
    
    $nginx -v
    nginx version: nginx/1.13.2
    
    $cat /etc/nginx/nginx.conf
    ...
    stream {
        upstream pod53{
            server 10.1.5.3:3306;
        }
        upstream pod54{
            server 10.1.5.4:3306;
        }
    
        map $server_addr $x {
            192.168.168.238 pod53;
            192.168.168.239 pod54;
        }
        server {
            listen 3306;
            proxy_pass $x;
        }
    }
    

    Thus, I can visit different MySQL service with the same port 3306 via different VIPs. Just like visiting different HTTP service with the same port via diffrent server_name.

    192.168.168.238 -> 10.1.5.3
    192.168.168.239 -> 10.1.5.4
    

提交回复
热议问题