在创建网络负载均衡器时,在“目标组”设置中,选择“实例”,然后启用“高级设置”并选择“启用跟踪客户端IP自动填充”选项。这将在HTTP头中添加X-Forwarded-For标头,以便实例可以使用源IP地址来响应请求。
示例代码:
resource "aws_lb_target_group" "example_group" { name = "example-group" port = 80 protocol = "HTTP" target_type = "instance"
health_check { interval = 30 path = "/" port = "traffic-port" protocol = "HTTP" timeout = 10 healthy_threshold = 2 unhealthy_threshold = 2 }
tags = { Name = "example-group" }
ip_address_type = "ipv4" stickiness { enabled = false type = "lb_cookie" }
target_group_attributes { key = "proxy_protocol_v2.enabled" value = "true" } }