首页 小组 文章 搜索 用户

[Haproxy]Haproxy让后端nginx RS记录真实IP(转贴)

2020-11-10 10:58:20
0
121

一、修改haproxy.cfg配置文件(注意路径),在defaults中加入如下两行,并重启haproxy。

vi /etc/haproxy/haproxy.cfg 

添加如下代码

defaults 
option http-server-close 
option forwardfor 

保存并退出后重启haproxy

service haproxy restart 

复制代码
二、修改nginx日志文件格式,并重启nginx。
注:nginx编译的时候需要有--with-http_realip_module此参数 (注意你的nginx路径)

vi /etc/nginx/nginx.conf 

添加以下代码

log_format main '{"Request_time":"$time_local",' 
'"Request_Real_Client":"$remote_addr",' 
'"Request_Mothod":"$request",' 
'"Forward_Real_WebServer_Status_Code":"$status",' 
'"Request_Size":"$body_bytes_sent",' 
'"Request_Http_Referer":"$http_referer"}' 
'"Request_User_Agent":"$http_user_agent",' 
'"Forward_Real_WebServer":"$http_x_forwarded_for"'; 

保存退出后重启nginx

service nginx restart 

三、请求日志格式

{"Request_time":"12/Jun/2019:14:52:47 +0800","Request_Real_Client":"192.168.7.80","Request_Mothod":"GET / HTTP/1.1","Forward_Real_WebServer_Status_Code":"304", 
"Request_Size":"0","Request_Http_Referer":"-"}"Request_User_Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/74.0.3729.169 Safari/537.36","Forward_Real_WebServer":"192.168.1.50" 

完成
来源于:https://www.cnblogs.com/cyleon/p/11009742.html

评论