首页 > 解决方案 > 代理 nginx 后面的 Jhipster 存储本地服务器 ip 而不是审计表中的请求 IP

问题描述

我在 NGINX 代理后面设置了一个带有 JHipster 4.10.2 的 tomcat,一切正常,但是当我转到审核视图以显示登录用户时……存储的 IP 始终是服务器 IP。有时存储的 IP 地址是 localhost IP 地址 127.0.0.1,有时是服务器的公共 IP,而不是登录到应用程序的客户端/用户 IP。

我测试了 java 代码以从请求中获取 IP 地址:

request.getHeader("X-FORWARDED-FOR")

这样我就得到了正确的 IP 地址(来自拨打电话的用户的真实 IP 地址)但是我不使用我可以修改或扩展的 Java 类来使用这种方式获取 IP 地址。

我在 application.yml 和 application-prod.yml 上使用 use-forward-headers 标志为 true

server.use-forward-headers = true

nginx配置:

location /myapp {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://localhost:8080/myapp;
    index  index.html index.htm;
}

标签: javaspring-bootnginxspring-securityjhipster

解决方案


请尝试 $http_x_real_ip,我从https://easyengine.io/tutorials/nginx/forwarding-visitors-real-ip得到了答案

proxy_set_header X-Forwarded-For $http_x_real_ip;

推荐阅读