配置nginx
在location节点下面加入如下代码
1
2
3
4
|
proxy_set_header Host $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; |
修改tomcat的server.xml配置文件
在Tomcat中要记录来访者真实IP,大家参考上面所述的tomcat日志配置语法,只需在日志模式中添加如下模式就行了:%{X-Real-IP}i
找到如下配置
1
2
3
|
< Valve className = "org.apache.catalina.valves.AccessLogValve" directory = "logs"
prefix = "localhost_access_log." suffix = ".txt"
pattern = "%h %l %u %t "%r" %s %b" />
|
修改成如下即可
1
2
3
|
< 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" />
|
查看tomcat访问记录,有记录客户端IP地址
番外:
设置记录来访客户真实IP与客户访问地址如下:
%h 访问的用户IP地址。(不是客户端IP地址的哦,要想在Tomcat中要记录来访者真实IP,只需在日志模式中添加如下模式就行了:%{X-Real-IP}i)
%h 访问的用户IP地址,如果nginx代理,那记录的就是nginx那台服务器IP,就是内网IP
1
2
3
|
< Valve className = "org.apache.catalina.valves.AccessLogValve" directory = "logs" prefix = "localhost_access_log." suffix = ".txt" pattern = "%{X-Real-IP}i %h %l %u %t "%r" %s %b %T" resolveHosts = "false"
fileDateFormat = "yyyy-MM-dd" />
|
本文转自ling118 51CTO博客,原文链接:http://blog.51cto.com/meiling/1979320,如需转载请自行联系原作者