前言
在jsp服务的时候,一般都是使用nginx+tomcat,后端的tomcat记录访问者的真实IP,nginx就需要进行一些配置,不然默认记录的都是nginx所在的服务器的IP。
配置
1、nginx配置转发头
在Nginx的server主机配置段中添加:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
说明:上面两行用于向tomcat发送真实的远端主机名和IP地址。其中的Host代表主机名, X-Real-IP代表主机IP,对于HTTP头部内容,这些变量是不区分大小写的。
2、tomcat配置日志格式
在Tomcat中要记录来访者真实IP,大家参考上面所述的tomcat日志配置语法,只需在日志模式中添加如下模式就行了:%{X-Real-IP}i
如下面完整的Tomcat日志配置段:
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="c:/wwwlogs/" prefix="cluster." suffix=".log"
pattern="%{X-Real-IP}i %u %t %r %s %b" resolveHosts="false" />
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.