Nginx与真实IP
配置了Nginx,Tomcat中的Web程序,获得的ip一直是“127.0.0.1”,比较纳闷。
获得远程ip,已经判断了很多情况,为什么会这样呢?
正解
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Java代码直接取:request.getRemoteAddr();
再有Nginx等情况下,获得的地址不对,比如“127.0.0.1”。
/**
*
* @author fansunion@qq.com 2014年12月7日
*/
public class IpUtil {
public static String getRemoteIp(HttpServletRequest request) {
String remoteIp = request.getHeader("x-forwarded-for");
if (remoteIp == null || remoteIp.isEmpty()
|| "unknown".equalsIgnoreCase(remoteIp)) {
remoteIp = request.getHeader("X-Real-IP");
}
if (remoteIp == null || remoteIp.isEmpty()
|| "unknown".equalsIgnoreCase(remoteIp)) {
remoteIp = request.getHeader("Proxy-Client-IP");
}
if (remoteIp == null || remoteIp.isEmpty()
|| "unknown".equalsIgnoreCase(remoteIp)) {
remoteIp = request.getHeader("WL-Proxy-Client-IP");
}
if (remoteIp == null || remoteIp.isEmpty()
|| "unknown".equalsIgnoreCase(remoteIp)) {
remoteIp = request.getHeader("HTTP_CLIENT_IP");
}
if (remoteIp == null || remoteIp.isEmpty()
|| "unknown".equalsIgnoreCase(remoteIp)) {
remoteIp = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (remoteIp == null || remoteIp.isEmpty()
|| "unknown".equalsIgnoreCase(remoteIp)) {
remoteIp = request.getRemoteAddr();
}
if (remoteIp == null || remoteIp.isEmpty()
|| "unknown".equalsIgnoreCase(remoteIp)) {
remoteIp = request.getRemoteHost();
}
if (remoteIp != null && remoteIp.indexOf(",") != -1) {
remoteIp = remoteIp.substring(remoteIp.lastIndexOf(",") + 1,
remoteIp.length()).trim();
}
return remoteIp;
}
}
Nginx与真实IP的更多相关文章
- ASP.NET Core 搭配 Nginx 的真实IP问题
一.前言 Nginx(Engine X)是一个高性能HTTP和反向代理服务,是由俄罗斯人伊戈尔·赛索耶夫为访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发 ...
- nodejs+nginx获取真实ip
nodejs + nginx获取真实ip分为两部分: 第一.配置nginx: 第二.通过nodejs代码获取: 其他语言也是一样的,都是配置nginx之后,在http头里面获取“x-forwarded ...
- Nginx 获取真实 IP 方案
问题根源: 基于七层的负载均衡系统,获取IP的原理都是通过XRI和XFF进行处理,从中选出“正常情况下”的源头IP,然而这两个Header都是普通的HTTP头,任何代理程序都可以轻易修改伪造它们,使得 ...
- 首层nginx 传递 二级代理,三级代理......多级代理nginx 客户端真实IP的方法
首层nginx(172.25.10.1):先获取真实IP($remote_addr),再将真实IP传递给X-Forwarded-For proxy_set_header X-Real-IP $r ...
- nginx 获取真实ip
使用阿里云SLB,无法获取真实ip问题 官方给出的是如下用法,需要安装模块,大体上是没有错的,但是比较模糊,实际操作中可能会踩坑,所以参考学习即可,不必照搬.(那个http_realip_module ...
- 阿里云负载均衡SLB 七层https协议 nginx 获取真实IP
https://www.cnblogs.com/baylorqu/p/8565667.html https://help.aliyun.com/document_detail/54007.html
- NGINX反向代理,后端服务器获取真实IP
一般使用中间件做一个反向代理后,后端的web服务器是无法获取到真实的IP地址. 但是生产上,这又是不允许的,那么怎么解决? 1.在NGINX反向代理服务器上进行修改 2.修改后端web服务器配置文件 ...
- 2015年工作中遇到的问题:81-90,标题党-Nginx与真实IP-Mybatis等
81."标题党"与"百度收录"问题. 很久以来,就发现那些"标题党"的收录和排名情况非常好,比如CSDN某篇文章,就随便一写,就排在了第一 ...
- 后端Apache获取前端Nginx反向代理的真实IP地址 (原创贴-转载请注明出处)
====================说在前面的话==================== 环境:前段Nginx是反向代理服务器:后端是Apache是WEB项目服务器 目的:让后端Apapche获取 ...
随机推荐
- js课程 6-15 js简单弹力球如何实现
js课程 6-15 js简单弹力球如何实现 一.总结 一句话总结:a.通过document的documentElement属性获取可是区域的高: b.通过增值变为负的实现到底部后反弹 1.docume ...
- Docker安装RabbitMQ,RabbitMQ Management使用
原文:Docker安装RabbitMQ,RabbitMQ Management使用 版权声明:本文为博主原创文章,未经博主允许不得转载.需要转载请先评论或者邮箱联系我,谢谢! https://blog ...
- MSDN上的异步socket 服务端例子
MSDN上的异步socket 服务端例子 2006-11-22 17:12:01| 分类: 代码学习 | 标签: |字号大中小 订阅 Imports SystemImports Syste ...
- 限制tomcat仅响应本机请求(转)
http://blog.bbzhh.com/index.php/archives/135.html 在VPS上搭建了nginx和tomcat应用,想通过nginx来反向代理127.0.0.1:8080 ...
- iOS 之应用性能调优的25个建议和技巧
注意:每在优化代码之前,你都要注意一个问题,不要养成"预优化"代码的错误习惯. 时常使用Instruments去profile你的代码来发现须要提升的方面.Matt Gallowa ...
- Day2:字典
一.定义 字典是一种“key-value”成对出现的数据类型,中间用冒号把key与value隔,不同的数据用逗号隔开,全部数据用大括号括起来 info = { 'stu1101': "Ten ...
- dinic算法学习(以poj1273为例)
Dinic 算法模板 Dinic算法是一种比較easy实现的.相对照较快的最大流算法. 求最大流的本质,就是不停的寻找增广路径.直到找不到增广路径为止. 对于这个一般性的过程,Dinic算法的优化例 ...
- [TypeScript] Sharing Class Behavior with Inheritance in TypeScript
Typescript classes make inheritance much easier to write and understand. In this lesson we look into ...
- 【39.66%】【codeforces 740C】Alyona and mex
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [PReact] Handle Simple Routing with preact-router
Some applications only need a very minimal routing solution. This lesson will cover a practical exam ...