IP透传

web服务器中需要记录客户端的真实IP地址,用于做访问统计,安全防护,行为分析,区域排行等场景.

Layer 4 与 Layer 7

四层:IP+PORT转发

七层:协议+内容交换

四层负载
在LVS 传统的四层负载设备中,把client发送的报文目标地址(原来是负载均衡设备的IP地址),根据均衡 设备设置的选择web服务器的规则选择对应的web服务器IP地址,这样client就可以直接跟此服务器建立 TCP连接并发送数据,而四层负载自身不参与建立连接 
 而和LVS不同,haproxy是伪四层负载均衡,因为haproxy 需要分别和前端客户端及后端服务器建立连接
七层代理
七层负载均衡服务器起了一个反向代理服务器的作用,服务器建立一次TCP连接要三次握手,而client要 访问Web Server要先与七层负载设备进行三次握手后建立TCP连接,把要访问的报文信息发送给七层负 载均衡;然后七层负载均衡再根据设置的均衡规则选择特定的 Web Server,然后通过三次握手与此台 Web Server建立TCP连接,然后Web Server把需要的数据发送给七层负载均衡设备,负载均衡设备再把 数据发送给client;所以,七层负载均衡设备起到了代理服务器的作用,七层代理需要和Client和后端服 务器分别建立连接
四层IP透传
haproxy配置
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg 
listen WEB_PORT_80
   mode tcp                                   #不支持http协议
   bind 192.168.10.100:80
   #cookie WEBSRV insert nocache indirect     #四层不支持cookie
   server web1 10.0.0.101:80 weight 1 check addr 10.0.0.101 port 80 
   server web2 10.0.0.102:80 weight 1 check addr 10.0.0.101 port 80 
[root@ubuntu2004 ~]#systemctl restart haproxy.service  
[root@ubuntu2004 ~]#curl www.meng.org
web1.meng.org 10.0.0.101
[root@ubuntu2004 ~]#curl www.emng.org
web2.meng.org 10.0.0.102

123机器测试下载,查看连接情况
[root@ubuntu2004 ~]#wget --limit-rate 1024 www.wang.org/f1.img
haproxy机器查看连接情况
[root@ubuntu2004 ~]#ss -nt
State  Recv-Q  Send-Q       Local Address:Port         Peer Address:Port      Process     
ESTAB     0     20160       192.168.10.100:80         192.168.10.123:52128              
ESTAB  885888     0         10.0.0.100:35666          10.0.0.101:80                    
ESTAB     0      36         10.0.0.100:22             10.0.0.1:50469 

可看到客户端192.168.10.123:52128连接haproxy的192.168.10.100:80 
haproxy的10.0.0.100:35666去连接后端的 10.0.0.101:80

客户端传过来就转过去了,不参与握手 

在后端服务器,是看不到客户端的地址的,只能看到haproxy的地址
[root@ubuntu2004 ~]#cat /var/log/nginx/access.log
10.0.0.100 - - [25/Oct/2022:21:23:56 +0800] "GET /f1.img HTTP/1.1" 200 1951634 "-" "Wget/1.20.3 (linux-gnu)"
后端nginx如何看到前端客户端的真是地址
第一步:haproxy配置
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
   mode tcp
   bind 192.168.10.100:80
   server web1 10.0.0.101:80 weight 1 check addr 10.0.0.101 port 80 send-proxy
   server web2 10.0.0.102:80 weight 1 check addr 10.0.0.101 port 80 send-proxy
[root@ubuntu2004 ~]#systemctl restart haproxy.service

第二步:后端nginx配置
[root@ubuntu2004 ~]#vim /etc/nginx/sites-enabled/default 
server {
    listen 80 default_server proxy_protocol;
    listen [::]:80 default_server;
检查语法加载
[root@ubuntu2004 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ubuntu2004 ~]#nginx -s reload

第三步:修改nginx日志格式
[root@ubuntu2004 ~]#vim /etc/nginx/nginx.conf
http {
   log_format main  '$remote_addr - $remote_user [$time_local] "$request" "$proxy_protocol_addr"'

    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log;
检查语法加载
[root@ubuntu2004 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ubuntu2004 ~]#nginx -s reload    

第四步:前端客户端123机器进行访问可看到真实IP
七层IP透传

当haproxy工作在七层的时候,也可以透传客户端真实IP至后端服务器

后端 web 服务器日志格式配置
nginx 日志格式
$proxy_add_x_forwarded_for:包括客户端IP和中间经过的所有代理的IP 
$http_x_forwarded_For : 只有客户端IP

[root@ubuntu2004 ~]#vim /etc/nginx/nginx.conf
http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" "$proxy_add_x_forwarded_for"'

    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log;
[root@ubuntu2004 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ubuntu2004 ~]#nginx -s reload

客户端123机器访问
[root@ubuntu2004 ~]#curl www.meng.org
web1.meng.org 10.0.0.101
后端服务器查看访问日志
[root@ubuntu2004 ~]#tail -f /var/log/nginx/access.log 
10.0.0.100 - - [25/Oct/2022:22:26:13 +0800] "GET / HTTP/1.1" "192.168.10.123, 10.0.0.100"sendfileon
之所以可以透传是因为haproxy主配置文件的defaults开启了 option forwardfor
报文修改