Skip to content

Instantly share code, notes, and snippets.

@penglei
Created July 30, 2018 12:42
Show Gist options
  • Save penglei/3216805a5df1b2f2374c3a17d1459aa0 to your computer and use it in GitHub Desktop.
Save penglei/3216805a5df1b2f2374c3a17d1459aa0 to your computer and use it in GitHub Desktop.
vip-by-iptables-demo
#!/bin/bash
iptables -t nat -N VIP-SERVICES
iptables -t nat -N VIP-DISTRIBUTION-NGINX
iptables -t nat -N VIP-DISTRIBUTION-NGINX-1
iptables -t nat -N VIP-DISTRIBUTION-NGINX-2
iptables -t nat -N VIP-POSTROUTING
iptables -t nat -I PREROUTING -j VIP-SERVICES
iptables -t nat -I OUTPUT -j VIP-SERVICES
iptables -t nat -A VIP-SERVICES -p tcp -m tcp --dport 443 -j VIP-DISTRIBUTION-NGINX
iptables -t nat -A VIP-DISTRIBUTION-NGINX -m statistic --mode random --probability 0.50000000000 -j VIP-DISTRIBUTION-NGINX-1
iptables -t nat -A VIP-DISTRIBUTION-NGINX -j VIP-DISTRIBUTION-NGINX-2
iptables -t nat -A VIP-DISTRIBUTION-NGINX-1 -p tcp -m tcp -j DNAT --to-destination "100.125.120.34:443"
iptables -t nat -A VIP-DISTRIBUTION-NGINX-2 -p tcp -m tcp -j DNAT --to-destination "100.125.120.35:443"
iptables -t nat -A POSTROUTING -o eth0 -j VIP-POSTROUTING
iptables -t nat -A VIP-POSTROUTING -d "100.125.120.0/24" -p tcp -m tcp -j MASQUERADE
output_iface=eth0
echo 1 > /proc/sys/net/ipv4/conf/$output_iface/route_localnet
@penglei
Copy link
Author

penglei commented Jul 30, 2018

访问本机 443 端口全部转发到 100.125.120.34100.125.120.35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment