IP透传是web服务器中需要记录客户端的真实IP地址,用于做访问统计、安全防护、行为分析、区域排行等场景。
四层IP透传配置
haproxy 配置: listen web_prot bind 192.168.32.204:80 mode tcp balance roundrobin server web1 10.0.0.201 send-proxy check inter 3000 fall 3 rise 5 nginx配置: server { listen 80 proxy_protocol; #listen 80; server_name 10.0.0.201; ......
七层IP透传配置
haproxy 配置: defaults option forwardfor 或者: option forwardfor header X-Forwarded-xxx #自定义传递IP参数,后端web服务器写X-Forwarded-xxx,如 果写option forwardfor则后端服务器web格式为X-Forwarded-For listen配置: listen web_host bind 192.168.32.204:80 mode http log global balance random server web1 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5 server web2 10.0.0.202:80 weight 1 check inter 3000 fall 2 rise 5
web服务器日志格式配置
配置web服务器,记录负载均衡透传的客户端IP地址
#apache 配置: LogFormat "%{X-Forwarded-For}i %a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User- Agent}i\"" combined #tomcat 配置: pattern='%{X-Forwarded-For}i %l %T %t "%r" %s %b "%{User-Agent}i"'/> #nginx 日志格式: log_format main '"$http_x_forwarded_For" - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" ';