java - 代理 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;
}
解决方案
请尝试 $http_x_real_ip,我从https://easyengine.io/tutorials/nginx/forwarding-visitors-real-ip得到了答案
proxy_set_header X-Forwarded-For $http_x_real_ip;
推荐阅读
- segmentation-fault - 如何使用 jpegoptim 多线程,跳过失败
- javascript - 在mapbox gl js中突出显示(更改颜色)道路、人行道或自行车道
- c# - 为什么设置 comboBox.SelectedIndex 无法更改 ComboBox 的可编辑部分中显示的内容?
- python - 如何让 shell 在 Excel 上的 VBA 上保持打开状态?
- excel - 刷新 Excel 中的所有 Access 数据连接取消保护和保护工作簿错误
- visual-studio - TFS:在两个分支之间合并 .csproj 文件,两个分支之间有项目引用
- asp.net-mvc - 如何在 AMP 和 .NET 中处理 IF 语句
- php - 更改附件名称:wp_mail PHP
- postgresql - 在 docker 容器中运行 pg_dump 并将文件输出到主机
- xslt - 将 XML 元素的名称替换为任意位置的属性值