多IP+端口流量分发全方案


🚀 多IP+端口流量分发全方案 | 智能路由与高可用架构
以下是实现单域名映射多IP+端口的专业级解决方案,涵盖DNS层、代理层、应用层的协同配置:


一、基础架构设计

                            [www.example.com]
                                   |
                   +---------------+---------------+
                   |               |               |
             [DNS轮询]       [反向代理]        [SRV记录]
                   |               |               |
             10.0.1.1:80    10.0.1.1:8080     _http._tcp.example.com
             10.0.1.2:8080  10.0.1.2:80           → 10.0.1.3:8888

二、核心实现方案

方案1:DNS轮询 + 端口映射(基础版)

适用场景:简单负载均衡,无会话保持需求

# DNS解析配置(以Cloudflare为例)
A记录 www → 10.0.1.1 (端口80)  
A记录 www → 10.0.1.2 (端口8080) → 需配合端口转发规则

# 服务器端配置(IPtables端口转发)
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 10.0.1.1:80  
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 10.0.1.2:8080

方案2:Nginx反向代理(推荐方案)

适用场景:复杂路由、SSL卸载、会话保持

upstream backend {
    # 定义多后端IP+端口
    server 10.0.1.1:80 weight=3; 
    server 10.0.1.2:8080 max_fails=2;
    server 10.0.1.3:8888 backup;
    
    # 健康检查
    check interval=3000 rise=2 fall=3 timeout=1000 type=http;
    check_http_send "HEAD /health HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;
}

server {
    listen 80;
    server_name www.example.com;
    
    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        
        # 会话保持(可选)
        hash $remote_addr consistent;
    }
}

方案3:SRV记录(高级协议支持)

适用场景:非HTTP协议(如WebSocket、游戏服务器)

# DNS SRV记录配置
_http._tcp.example.com. 3600 IN SRV 10 50 80 server1.example.com.
_http._tcp.example.com. 3600 IN SRV 20 50 8080 server2.example.com.

三、动态路由策略

1. 基于地理位置的流量分发

geo $geoip_country_code $backend_pool {
    default        backend_global;
    CN             backend_cn;
    US             backend_us;
}

upstream backend_global { server 10.0.2.1:80; }
upstream backend_cn     { server 10.0.3.1:8080; }
upstream backend_us     { server 10.0.4.1:8880; }

2. 权重动态调整(API控制)

# 实时修改Nginx权重
curl -X PATCH -d "{\"server\":\"10.0.1.1:80\",\"weight\":5}" \
     http://nginx-api/upstream/backend

四、健康检查与故障转移

层级       检测方式              超时时间   恢复策略
----------------------------------------------------------
L4       TCP端口探测            2s        连续失败3次标记不可用
L7       HTTP状态码校验         5s        自动剔除异常节点
业务层   自定义脚本检测         10s       触发报警并隔离

配置示例(Nginx Plus)

health_check {
    uri /api/health;
    interval 5s;
    passes 2;
    fails 3;
    match status_ok {
        status 200;
        header Cache-Control ~ "max-age=3600";
    }
}

五、安全增强措施

1. 端口隐匿技术

# 仅允许来自负载均衡器的访问
location / {
    allow 10.0.0.0/24;  # LB IP段
    deny all;
    ...
}

2. 动态防火墙规则

# 自动屏蔽异常IP(与Fail2Ban集成)
fail2ban-client set nginx-badrequest banip 192.168.1.100

六、测试与验证

1. 流量分发测试

# 模拟100次请求观察分布
for i in {1..100}; do 
    curl -s http://www.example.com/api/instance
done | sort | uniq -c

2. 故障转移测试

# 关闭第一个后端服务
systemctl stop nginx

# 观察日志中的自动切换
tail -f /var/log/nginx/error.log | grep 'marking backend down'

📌 注意事项

  • DNS缓存问题:TTL建议设为300秒(5分钟)
  • SSL证书管理:建议在反向代理层统一处理HTTPS
  • 端口冲突:确保后端服务的端口在防火墙中开放
消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息