尧图网络 高端网站定制 · 原创设计
免费咨询热线
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/28 0:27:50
NV-Q-Link 相关的代码信息收集

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

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

📅 2026/9/28 0:28:09
从B站视频到个人音乐库:BilibiliDown音频提取完全指南

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

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

📅 2026/9/28 0:59:40
MORE NEWS

更多资讯

📰

清镇电工证在哪学?实操考试心里没底怕挂科?手把手教你搞懂

清镇电工证在哪学?实操考试心里没底怕挂科?手把手教你搞懂 实操考试心里没底怕挂科?你不是一个人。清镇电工证在哪学?这个问题,几乎每个打算报考电工证的人都问过,尤其是在实操考试前,很多人都会担心自己能不能一次性通过。但只要你找对了地方,了解清…

📰

北海可以考电工证么?2026河南最新政策下怎么避坑?

北海可以考电工证么?2026河南最新政策下怎么避坑? 你是不是也遇到过这种情况:想找地方考个电工证,结果一问三不知,中介又乱收费?2026年河南最新政策已经出台,北海可以考电工证么?别急,我来给你讲清楚。…

📰

东莞市安监局考电工证过期了不知道怎么复审延期?值不值得考?

东莞市安监局考电工证过期了不知道怎么复审延期?值不值得考? 证书过期了不知道怎么复审延期?你是不是也遇到过这种情况?特别是在东莞从事电工工作的朋友,一旦电工证过期了,不仅影响日常工作,甚至可能面临被单位清退的风险。这年头,…

📰

陈斌电工证怎么选?工地太忙根本没时间复习考试,考前押题帮你解决

陈斌电工证怎么选?工地太忙根本没时间复习考试,考前押题帮你解决 工地太忙根本没时间复习考试,你是不是也经常这样想?尤其是像我们这样的初次报考陈斌电工证的南阳小伙伴,既要兼顾日常作业,又要抽时间备考,压力真的不小。别急,现在考前押题已经成了很…

📰

胶州高压电工证培训保姆级教程:实操考试心里没底怕挂科?看这篇就够了

胶州高压电工证培训保姆级教程:实操考试心里没底怕挂科?看这篇就够了 实操考试心里没底怕挂科?你不是一个人。作为胶州一家中小型施工企业的负责人,我带过的电工团队里,有太多人因为高压电工证考试没通过,耽误了项目进度,还影响了企业声誉。今天,我就…

📰

电工证低压需要考吗?考前押题怎么用?别让证书过期耽误你

电工证低压需要考吗?考前押题怎么用?别让证书过期耽误你 证书过期了不知道怎么复审延期?你不是一个人。尤其是低压电工证,很多人以为低压作业风险小,就不重视考证,结果一旦证书到期,不仅影响工作,还可能面临违章操作的风险。别急,今天就帮你把“电工…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

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

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

📞 💬