获取IP地址为 127.0.0.1, 或者内网地址
Nginx配置, 只有 proxy_pass 时只能获取到 127.0.0.1
location / {proxy_pass http://127.0.0.1:8080;
}
修改为
location / {#保留代理之前的host 包含客户端真实的域名和端口号proxy_set_header Host $host; #保留代理之前的真实客户端ipproxy_set_header X-Real-IP $remote_addr; #这个Header和X-Real-IP类似,但它在多级代理时会包含真实客户端及中间每个代理服务器的IPproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#表示客户端真实的协议(http还是https)proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://127.0.0.1:8080;}
获取IP地址
pom, hutool
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.20</version>
</dependency>
String clientIP = ServletUtil.getClientIP(request);