Nginx反向代理时Tomcat日志记录真实IP
nginx反向代理时,tomcat日志默认记录的IP是127.0.0.1,如果要记录真实IP,可以用下面的方式修改。
修改nginx配置
在nginx配置文件的proxy_pass同级代码块中,设置一些请求头,添加:
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Public-Network-URL http://$http_host$request_uri;
修改tomcat配置
将tomcat配置文件server.xml中日志内容的IP地址替换为nginx设置的请求头X-Real-IP
tomcat默认的配置是:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
修改为:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%{X-Real-IP}i %l %u %t "%r" %s %b" />
Reference
https://blog.csdn.net/qq_40460909/article/details/81781610
https://blog.51cto.com/weijixian1015/823125
https://codeantenna.com/a/8ebtZe7Jp1
发表评论