最近更新时间:2023.09.13 15:22:43
首次发布时间:2023.03.07 10:29:25
本文为您介绍四层监听器如何获取访问负载均衡服务的客户端源IP地址。
负载均衡的四层监听器(TCP/UDP协议)默认具有源地址透传功能,报文经过CLB后客户端源IP地址不会被替换,您可以直接获取到源IP地址。
如果后端服务器组为IP类型,或者客户端IP地址为IPv6地址,而后端服务器为IPv4地址,客户端请求通过负载均衡时,负载均衡会对客户端IP地址进行转换,从而导致后端服务器无法直接获取客户端的源IP地址。
此时,您可以通过启用四层监听器的Proxy Protocol协议功能来获取客户端源IP地址。
说明
四层监听器的Proxy Protocol协议功能正在邀测中,如需使用,请提交工单或联系客户经理申请。
vi /etc/nginx/nginx.conf
http { ... server { listen 80 proxy_protocol; listen 443 ssl proxy_protocol; ... } ... stream { ... server { listen 123 proxy_protocol; // 配置TCP/UDP协议的Proxy Protocol功能 ... } }
systemctl start nginx.service
通过cat /var/log/nginx/access.log
命令查看访问日志,可以获取客户端的源IP地址。
/* * Proxy Protocol Header: * NAT44 Header * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | | * | Signature | * | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Ver | Cmd | Af | Proto | Length | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | IPv4 Source Address | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | IPv4 Destination Address | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Source Port | Destination Port | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */
/* * Proxy Protocol Header: * NAT66 Header * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | | * | Signature | * | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Ver | Cmd | Af | Proto | Length | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | | * | | * | IPv6 Source Address | * | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | | * | | * | IPv6 Destination Address | * | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Source Port | Destination Port | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */