站长刚入门JSP,想获取用户IP,调用函数发现获取的IP为127.0.0.1。
- 架构:Nginx反向代理Tomcat
首先修改Nginx的配置文件,在反向代理处添加如下代码:
proxy_set_header Host test.paolu.host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
然后修改Tomcat的配置,在<Host></Host>
块内添加:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="x-forwarded-for"
proxiesHeader="x-forwarded-by"
protocolHeader="x-forwarded-proto" />
</Host>
然后重启Tomcat即可