尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
实战指南:HAProxy 七层与四层源 IP 透传配置详解
web服务器中需要记录客户端的真实IP地址用于做访问统计、安全防护、行为分析、区域排行等场景七层IP透传#实验环境[roothaproxy ~]# vim /etc/haproxy/haproxy.cfglisten webclusterbind*:80 balance roundrobin server haha192.168.0.10:80 check inter 3s fall3rise5weight1server hehe192.168.0.20:80 check inter 3s fall3rise5weight1[roothaproxy ~]# systemctl restart haproxy.service#测试环境[Administrator.DESKTOP-VJ307M3]➤foriin{1..5}docurl172.25.254.100donewebserver1 -192.168.0.10 webserver2 -192.168.0.20 webserver1 -192.168.0.10 webserver2 -192.168.0.20 webserver1 -192.168.0.10#在apache主机中默认是未开启透传功能的/nginx默认开启ip透传[rootwebserver2 ~]# cat /etc/httpd/logs/access_log192.168.0.100 - -[26/Jan/2026:10:03:03 0800]GET / HTTP/1.120026-curl/7.65.0192.168.0.100 - -[26/Jan/2026:10:03:03 0800]GET / HTTP/1.120026-curl/7.65.0#开启ip透传的方式[roothaproxy ~]# vim /etc/haproxy/haproxy.cfg。。。忽略。。。。。 defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except127.0.0.0/8#开启haproxy透传功能option redispatch retries3timeouthttp-request 10stimeoutqueue 1mtimeoutconnect 10stimeoutclient 1mtimeoutserver 1mtimeouthttp-keep-alive 10stimeoutcheck 10s maxconn3000#在Apache中设定采集透传IP[rootwebserver2 ~]# vim /etc/httpd/conf/httpd.conf201LogFormat%h %l %u %t\%r\%s %b\%{X-Forwarded-For}i\\%{Referer}i\\%{User-Agent}i\combined[rootwebserver2 ~]# systemctl restart httpd#测试效果[rootwebserver2 ~]# cat /etc/httpd/logs/access_log192.168.0.100 - -[26/Jan/2026:10:10:29 0800]GET / HTTP/1.120026172.25.254.1-curl/7.65.0192.168.0.100 - -[26/Jan/2026:10:10:30 0800]GET / HTTP/1.120026172.25.254.1-curl/7.65.0192.168.0.100 - -[26/Jan/2026:10:10:30 0800]GET / HTTP/1.120026172.25.254.1-curl/7.65.0四层IP透传#环境设置#RS1中部署apache[rootwebserver1 ~]# dnf install httpd -y[rootwebserver1 ~]# echo RS2 - 192.168.0.10 /var/www/html/index.html[rootwebserver1 ~]# systemctl enable --now httpd#在RS2中部署nginx#部署nginx[rootwebserver2 ~]# dnf install nginx -y[rootwebserver2 ~]# echo RS2 - 192.168.0.20 /usr/share/nginx/html/index.html[rootwebserver2 ~]# systemctl enable --now nginx#测环境[Administrator.DESKTOP-VJ307M3]➤foriin{1..5};docurl172.25.254.100;doneRS1 -192.168.0.10 RS2 -192.168.0.20 RS1 -192.168.0.10 RS2 -192.168.0.20 RS1 -192.168.0.10#启用apache的四层访问控制[rootnode1 ~]# vim /etc/httpd/conf.modules.d/10-proxy_h2.confLoadModule proxy_http2_module modules/mod_proxy_http2.so LoadModule remoteip_module modules/mod_remoteip.so[rootnode1 ~]# vim /etc/httpd/conf/httpd.confRemoteIPProxyProtocol on RemoteIPTrustedProxy192.168.0.0/24#直接添加[rootnode1 ~]# systemctl restart httpd#启用nginx的四层访问控制[rootwebserver2 ~]# vim /etc/nginx/nginx.confserver{listen80proxy_protocol;#启用四层访问控制listen[::]:80;server_name _;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page404/404.html;location/404.html{}[rootwebserver2 ~]# systemctl restart nginx.service#测试Administrator.DESKTOP-VJ307M3]➤foriin{1..5};docurl172.25.254.100;donehtmlbodyh1502Bad Gateway/h1The server returned an invalid or incomplete response./body/htmlhtmlbodyh1502Bad Gateway/h1The server returned an invalid or incomplete response./body/htmlhtmlbodyh1502Bad Gateway/h1The server returned an invalid or incomplete response./body/htmlhtmlbodyh1502Bad Gateway/h1The server returned an invalid or incomplete response./body/htmlhtmlbodyh1502Bad Gateway/h1The server returned an invalid or incomplete response./body/html出现上述报错标识nginx只支持四层访问#设定haproxy访问4层[roothaproxy ~]# vim /etc/haproxy/haproxy.cfglisten webclusterbind*:80 mode tcp#四层访问balance roundrobin server haha192.168.0.10:80 send-proxy check inter 3s fall3rise5weight1server hehe192.168.0.20:80 send-proxy check inter 3s fall3rise5weight1#添加send-proxy[roothaproxy ~]# systemctl restart haproxy.service#测试四层访问[Administrator.DESKTOP-VJ307M3]➤foriin{1..5};docurl172.25.254.100;doneRS1 -192.168.0.10 RS2 -192.168.0.20 RS1 -192.168.0.10 RS2 -192.168.0.20 RS1 -192.168.0.10#设置4层ip透传[rootwebserver12~]# vim /etc/nginx/nginx.conflog_format main$remote_addr - $remote_user [$time_local] $request $proxy_protocol_addr#采集透传信息$status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for;[rootwebserver12~]# systemctl restart nginx.service#测试[Administrator.DESKTOP-VJ307M3]➤foriin{1..5};docurl172.25.254.100;doneRS2 -192.168.0.20 RS1 -192.168.0.10 RS2 -192.168.0.20 RS1 -192.168.0.10 RS2 -192.168.0.20[rootwebserver1 ~]# cat /var/log/nginx/access.log192.168.0.100 - -[26/Jan/2026:10:52:40 0800]GET / HTTP/1.1172.25.254.120019-curl/7.65.0-192.168.0.100 - -[26/Jan/2026:10:53:49 0800]GET / HTTP/1.1172.25.254.120019-curl/7.65.0-192.168.0.100 - -[26/Jan/2026:10:53:50 0800]GET / HTTP/1.1172.25.254.120019-curl/7.65.0-192.168.0.100 - -[26/Jan/2026:10:53:50 0800]GET / HTTP/1.1172.25.254.120019-curl/7.65.0-​
RELATED

相关推荐

电路增益全解析:从电压、电流、功率到设计实战

电路增益全解析:从电压、电流、功率到设计实战

1. 从“放大”说起:增益到底是什么?聊到电路,尤其是模拟电路或者射频电路,“增益”这个词几乎是绕不开的。很多刚入门的朋友一听到“增益”,第一反应就是“放大倍数”,这个理解方向是对的,但不够…

📅 2026/9/8 20:22:38
NV-Q-Link 相关的代码信息收集

NV-Q-Link 相关的代码信息收集

根据现有搜索结果,关于 NVQLink 的代码需要区分两个不同层面的含义:一、量子计算 NVQLink:代码在 CUDA-Q 仓库中 这是 NVIDIA 官方定义的 NVQLink 主体——一个用于连接 GPU 与量子处理器(QPU)的平台架构,其…

📅 2026/9/8 9:26:52
从B站视频到个人音乐库:BilibiliDown音频提取完全指南

从B站视频到个人音乐库:BilibiliDown音频提取完全指南

从B站视频到个人音乐库:BilibiliDown音频提取完全指南 【免费下载链接】BilibiliDown (GUI-多平台支持) B站 哔哩哔哩 视频下载器。支持稍后再看、收藏夹、UP主视频批量下载|Bilibili Video Downloader 😳 项目地址: https://gitcode.com/gh_mirrors/b…

📅 2026/9/6 1:33:31
MORE NEWS

更多资讯

📰

国内可以做的国外兼职网站进阶技巧

5个国内可做的国外兼职网站2026最新指南 改个需求建站公司拖一周,这种憋屈感谁懂?很多前端新手刚入行,盯着国内那些卷生卷死的接单平台,发现时薪低、沟通累、回款慢,心里直打鼓。其实换个思路,把目光投向海外, 国内可以做的国外兼职网站…

📰

网站被黑挂马?3步图解步骤搞定软件介绍下载网站建设安全

网站被黑挂马?3步图解步骤搞定软件介绍下载网站建设安全 上周一个做B2B外贸的客户急得跳脚,后台日志全是陌生的IP访问,首页弹出一堆赌博广告,服务器CPU飙满。他问:网站被黑挂马不知道怎么办?别慌,这是建站圈最常见的噩梦。我直接甩给他一份基…

📰

新手从零搭建网站促销活动策划避坑指南:3个方案费用全拆解

新手从零搭建网站促销活动策划避坑指南:3个方案费用全拆解 自己不会代码想做网站,是不是光听到“服务器配置”、“SSL证书”这些词就头大?别慌,这正是大多数企业老板和运营新手的真实处境。很多同行找过我做网站促销活动策划咨询,第一句话往往是:“…

📰

深圳技术支持骏域网站建设:3种方案报价拆解,告别网站没人看

深圳技术支持骏域网站建设:3种方案报价拆解,告别网站没人看 网站上线三个月,后台访问数据惨淡得让人想砸电脑。这种“做了没人看”的困境,比建站本身更让人头疼。很多老板以为砸钱就能搞定,结果发现 建站报价…

📰

3招搞定wordpress多程序用户同步,一文搞懂省钱逻辑

3招搞定wordpress多程序用户同步,一文搞懂省钱逻辑 网站做好了没人访问,是不是让你抓狂?明明砸了钱做建设,流量却像死水一样。很多安徽做B2B或者本地服务的老板,都踩过这个坑:官网是官网,商城是商城,后台是后台,用户注册了三次,体验差…

📰

Java IEC 62056-21 C模式主站协议库:从串口到TCP的能源数据采集实践

简介:面向Java开发者的IEC 62056-21 C模式主站协议库实现,可用于通过串口或网络从燃气表、水表、热量表、电表等能源计量装置读取标准化数据,解决多设备数据采集与协议解析难题,适合能源管理、智能家居及远程监控系统的集成开发。…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬