AWS ELB 后端获取真实客户端IP地址配置

版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。 https://blog.csdn.net/fgf00/article/details/80164200

一、背景:https ELB后端nginx获取不到真实客户端IP

使用aws elb服务器来做websocket负载均衡时,只能使用tcp模式。

https 协议不在也可以选择tcp模式,都有后端获取不到真实客户端IP问题,获取都是elb IP地址

代理协议是一种 Internet 协议,用于将连接信息从请求连接的源传递到请求连接到的目标。Elastic Load Balancing 使用代理协议版本 1,该版本使用用户可读的标头格式。

TCP 负载均衡其实是把客户端的请求截断,然后自己发送一个请求给后端,拿到后端返回的数据之后再返回给 客户端,这样后端看到的是 负载均衡器的IP,看不到客户端的真实IP了 (如果用基于HTTP的Load Balancer,会自动在HTTP头记录 X-Forwarded-For , 后端自然很容易获取到源IP )。其实这和LVS FULLNAT 模式有点像,LVS FULLNAT的解决办法是把真实IP写在TCP option里面,然后后端用toa模块拿到。

对于这个问题,AWS给了一个解决办法,叫做 Proxy Protocol ,可以对TCP 负载均衡器开启Proxy Protocol,它会在请求的第一行写入 源IP、源端口等信息,以 \r\n 结尾,格式如下:

PROXY_STRING + single space + INET_PROTOCOL + single space + CLIENT_IP + single space + PROXY_IP + single space + CLIENT_PORT + single space + PROXY_PORT + “\r\n”

二、安装 AWS CLI (Command Line Interface)

1、安装

pip install --upgrade --user awscli   # (win下:下载msi安装程序)

在安装 AWS CLI 后,将可执行文件路径添加到您的 PATH 变量中:

vim ~/.bash_profile
export PATH=~/.local/bin:$PATH  # ~/.local/bin 添加到当前 PATH 变量中
source ~/.bash_profile
aws --version

2、配置访问密钥

aws configure
# By default, its location is at ~/.aws/credentials:
[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

三、AWS ELB 启用代理协议 Proxy Protocol

1、为elb(名称 YOU_ELB_NAME) 创建一个Proxy Protocol,叫 EnableProxyProtocol

aws elb create-load-balancer-policy --load-balancer-name YOU_ELB_NAME --policy-name EnableProxyProtocol --policy-type-name ProxyProtocolPolicyType --policy-attributes AttributeName=ProxyProtocol,AttributeValue=True
# 注意 YOU_ELB_NAME 这里改为你的elb名称

2、为 YOU_ELB_NAME 激活 EnableProxyProtocol

aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 443 --policy-names EnableProxyProtocol

然后确认开启

aws elb describe-load-balancers --load-balancer-name YOU_ELB_NAME

3、查看是否启用

aws elb describe-load-balancers --load-balancer-name YOU_ELB_NAME | jq '.LoadBalancerDescriptions[].BackendServerDescriptions'

[
  {
    "PolicyNames": [
      "EnableProxyProtocol"
    ],
    "InstancePort": 80
  },
  {
    "PolicyNames": [
      "EnableProxyProtocol"
    ],
    "InstancePort": 443
  }
]

4、如果要禁用代理协议可以这么做,同时,可通过上一步查看是否禁用了。

aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 80 --policy-names "[]"

四、配置nginx接受代理协议头

nginx 从1.5.12 版本开始支持Proxy Protocol ,只需要配置一下就OK了

1、在http 里面配置如下:

set_real_ip_from   10.0.0.0/8;
# 表示把来在10.0.0.0/8 段(TCP负载均衡器的IP段)的所有请求的来源地址,都改成 $proxy_protocol_addr,并且记录在 $remote_addr 变量里。
real_ip_header     proxy_protocol;

这是日志格式:

        log_format  main  '$remote_addr $request_method $http_host $request_uri '
                        '$status $request_time $body_bytes_sent $upstream_response_time $http_referer '
                        '$upstream_cache_status $http_x_forwarded_for $http_user_agent';

2、在server里面linsten的时候开启 proxy_protocol

listen      80  proxy_protocol;
listen     443  proxy_protocol;

参考地址:

https://docs.aws.amazon.com/zh_cn/elasticloadbalancing/latest/classic/enable-proxy-protocol.html

http://www.ttlsa.com/web/hang-aws-elb-services/

扫描二维码关注公众号,回复: 3780033 查看本文章

http://www.ttlsa.com/nginx/aws-elb-nginx-enable-proxy-protocol/

猜你喜欢

转载自blog.csdn.net/fgf00/article/details/80164200
AWS