Haproxy下的web服务器记录用户真实IP

1)在haproxy配置文件中加入参数
listen www

option forwardfor #让web端记录的是客户端的ip而不是ha的ip

2)编辑apache服务器配置文件
vim /etc/httpd/conf/httpd.conf
LogFormat “”%{X-Forwarded-For}i\”%V %A %t “%r” %>s %b “%{Referer}i”"%{User-Agent}i"" combined
#LogFormat “”%{X-Forwarded-For}i" %l %t “%r” %>s %b"%{Referer}i" “%{User-Agent}i”" combined
cut -d " " -f 1 /etc/httpd/logs/access_log |sort|uniq -c|sort -rn -k1 #统计日志中ip访问

3)如果是后端RS是nginx则加入下面参数
set_real_ip_from ip;(这个ip填写的是haproxy的ip)真实服务器上一级代理的IP地址或者IP段,可以写多行 不记录指定的ip
real_ip_header X-Forwarded-For;从哪个header头检索出所要的IP地址
real_ip_recursive on;递归的去除所配置中的可信IP。排除set_real_ip_from里面出现的IP。如果出现了未出现这些IP段的IP,那么这个IP将被认为是用户的IP。

log_format main ‘$remote_addr – r e m o t e u s e r [ remote_user [ time_local] “ r e q u e s t request” ‘ ‘ status b o d y b y t e s s e n t body_bytes_sent “ http_referer” ‘
‘” h t t p u s e r a g e n t http_user_agent” “ http_x_forwarded_for”‘

location ~ .* {
proxy_pass http://192.168.56.8;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forward-For $remote_addr;
proxy_set_header Host $host;
set_real_ip_from 192.168.180.0/24;
set_real_ip_from 192.168.181.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
}

猜你喜欢

转载自blog.csdn.net/bjgaocp/article/details/88381447