在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在使用aws云服务的时候,90%要使用ELB服务作为负载均衡的解决方案,使用ELB要比自己搭建负载均衡要方便得多。 AWS ELB nginx 启用代理协议 PROXY_STRING + single space + INET_PROTOCOL + single space + CLIENT_IP + single space + PROXY_IP + single space + CLIENT_PORT + single space + PROXY_PORT + "\r\n" 实例: PROXY TCP4 198.51.100.22 203.0.113.7 35646 80\r\n 安装AWS CLI工具 # sudo apt-get install python-pip # sudo pip install awscli 配置授权连接参数文件。 # sudo vi ~/.aws/config [default] aws_access_key_id = YOU_ACCESS_ID aws_secret_access_key = YOU_SECRET_ID output = json OR bson OR text region = PREFERRED_AWS_REGION 类似这样的,aws_access_key_id、aws_secret_access_key、region根据你的aws实例填写。 # aws elb describe-load-balancer-policy-types { "PolicyTypeDescriptions": [ ... { "PolicyAttributeTypeDescriptions": [ { "Cardinality": "ONE", "AttributeName": "ProxyProtocol", "AttributeType": "Boolean" } ], "PolicyTypeName": "ProxyProtocolPolicyType", "Description": "Policy that controls whether to include the IP address and port of the originating request for TCP messages. This policy operates on TCP/SSL listeners only" }, ... ] } 创建启用代理协议的策略 # 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 该命令创建了一个名称为EnableProxyProtocol的策略,并分配下列ELB属性"AttributeName=ProxyProtocol & AttributeValue=True"。 # aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 80 --policy-names EnableProxyProtocol # aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 81 --policy-names EnableProxyProtocol # aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 443 --policy-names EnableProxyProtocol 此命令将替代当前已启用的策略组。因此,--policy-names 选项必须同时指定您正在添加到列表中的策略和任何当前已启用的策略。 # aws elb describe-load-balancers --load-balancer-name YOU_ELB_NAME | jq '.LoadBalancerDescriptions[].BackendServerDescriptions' [ { "PolicyNames": [ "EnableProxyProtocol" ], "InstancePort": 80 }, { "PolicyNames": [ "EnableProxyProtocol" ], "InstancePort": 81 }, { "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接受代理协议头 set_real_ip_from 127.0.0.1; set_real_ip_from 10.0.0.0/8; real_ip_header proxy_protocol; real_ip_recursive on; server { listen 80 proxy_protocol; listen 443 proxy_protocol ssl; ... location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $proxy_protocol_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto tcp; proxy_set_header X-NginX-Proxy true; ... } set_real_ip_from 127.0.0.1; set_real_ip_from 10.0.0.0/8; real_ip_header proxy_protocol; real_ip_recursive on; server { listen 80 proxy_protocol; listen 443 proxy_protocol ssl; ... location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $proxy_protocol_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto tcp; proxy_set_header X-NginX-Proxy true; ... } 当nginx启用了代理协议,$proxy_protocol_addr变量将是真实的客户端IP。 log_format elb_log '$proxy_protocol_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent "$http_referer" ' '"$http_user_agent"'; set_real_ip_from 172.31.0.0/20; set_real_ip_from 10.0.0.0/8; real_ip_header proxy_protocol; |
请发表评论