Getting Orgin IP From Load Balancer

2019-01-20 10:06发布

Is there a way to get the origin IP of the user from the HTTP load balancing w/ GCloud? We are currently using just Network Load Balancing, and are needing to move to a cross region balancer although we need to user's IP for compliance and logging.

Does it pass in a header or something along those lines?

Thanks ~Z

3条回答
▲ chillily
2楼-- · 2019-01-20 10:30

The documentation (https://cloud.google.com/compute/docs/load-balancing/http/) says it's the first IP address of the X-Forwarded-For header.

  X-Forwarded-For: <client IP(s)>, <global forwarding rule external IP>
查看更多
We Are One
3楼-- · 2019-01-20 10:38

If you are sure that you do not run any other proxy (that append additional IPs into X-Forwarded-For) behind Google Cloud Balancing, you can get the second to last IP from X-Forwarded-For as immediate client IP. Or even if you have some proxies but know the exact number of additional IPs that will be appended, you can also add those into account.

From https://cloud.google.com/compute/docs/load-balancing/http/#components:

X-Forwarded-For: <unverified IP(s)>, <immediate client IP>, <global forwarding rule external IP>, <proxies running in GCP> (requests only)

Only the <immediate client IP> and <global forwarding rule external IP> entries are provided by the load balancer. All other entries in the list are passed along without verification.

IPs that comes before immediate client IP could be spoofed IPs or IPs coming from client proxies. Even if the client spoofs X-Forwarded-For header, the load balancer still appends the actual IP that hits the load balancer.

查看更多
Rolldiameter
4楼-- · 2019-01-20 10:38

Ok, so after digging though headers and other things I found the following header that is passing the origin IP and thee IP for the user.

$_SERVER['HTTP_X_FORWARDED_FOR']

You will need to split it by the ',' and take the first part of the string. This is the user IP, that is being pushed by the Google Cloud HTTP Balancer.

查看更多
登录 后发表回答