Java开发中使用 request.getRemoteAddr 获取客户端 ip ,返回结果始终为127.0.0.1。原因是服务器使用了nginx反向代理。
解决办法:在nginx配置文件 nginx.conf 中添加:proxy_set_header X-Real-IP $remote_addr;
- server {
-
- location ^~ /testweb/ {
- root html;
- access_log on;
- index index.jsp;
- proxy_set_header X-Real-IP $remote_addr; //添加此项
- proxy_pass http://127.0.0.1:88/testweb/;
- }
-
- }
java 代码如下: