确定客户的亚马逊ELB背后的IP地址亚马逊、客户、地址、ELB

2023-09-11 10:59:30 作者:薄衫少年

我们对后面ELB EC2一些PHP服务器。我们想通过连接到我们的服务器的客户端的IP地址,以确定区域设置/地区。现在的问题是PHP的服务器只能看到ELB的IP地址。我们希望看到通过ELB通过客户端的IP地址。

We have some PHP servers on EC2 behind ELB. We would like to determine the locale/region by IP address of the clients connecting to our servers. The problem is the PHP servers only see the IP address of the ELB. We would like to see the IP addresses of the clients passed through the ELB.

推荐答案

按照AWS文档,该ELB应该设置的X - 转发,对于HTTP标头,preserves原始客户端IP地址:

According to the AWS docs, the ELB should be setting the 'X-Forwarded-For' HTTP header which preserves the original client ip address:

的X转发,对于请求头可帮助您识别客户端的IP地址。由于负载均衡器拦截客户端和服务器之间的流量,你的服务器访问日志中包含了负载均衡的唯一的IP地址。要查看客户端的IP地址,使用X - 转发,对于请求头。

The X-Forwarded-For request header helps you identify the IP address of a client. Because load balancers intercept traffic between clients and servers, your server access logs contain only the IP address of the load balancer. To see the IP address of the client, use the X-Forwarded-For request header.

您可以使用下面的PHP code访问它(假设阿帕奇):

You can access it using the following PHP code (assuming apache):

$http_headers = apache_request_headers(); 
$original_ip = $http_headers["X-Forwarded-For"];