配置了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的更多相关文章

  1. ASP.NET Core 搭配 Nginx 的真实IP问题

    一.前言 Nginx(Engine X)是一个高性能HTTP和反向代理服务,是由俄罗斯人伊戈尔·赛索耶夫为访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发 ...

  2. nodejs+nginx获取真实ip

    nodejs + nginx获取真实ip分为两部分: 第一.配置nginx: 第二.通过nodejs代码获取: 其他语言也是一样的,都是配置nginx之后,在http头里面获取“x-forwarded ...

  3. Nginx 获取真实 IP 方案

    问题根源: 基于七层的负载均衡系统,获取IP的原理都是通过XRI和XFF进行处理,从中选出“正常情况下”的源头IP,然而这两个Header都是普通的HTTP头,任何代理程序都可以轻易修改伪造它们,使得 ...

  4. 首层nginx 传递 二级代理,三级代理......多级代理nginx 客户端真实IP的方法

    首层nginx(172.25.10.1):先获取真实IP($remote_addr),再将真实IP传递给X-Forwarded-For    proxy_set_header X-Real-IP $r ...

  5. nginx 获取真实ip

    使用阿里云SLB,无法获取真实ip问题 官方给出的是如下用法,需要安装模块,大体上是没有错的,但是比较模糊,实际操作中可能会踩坑,所以参考学习即可,不必照搬.(那个http_realip_module ...

  6. 阿里云负载均衡SLB 七层https协议 nginx 获取真实IP

    https://www.cnblogs.com/baylorqu/p/8565667.html https://help.aliyun.com/document_detail/54007.html

  7. NGINX反向代理,后端服务器获取真实IP

    一般使用中间件做一个反向代理后,后端的web服务器是无法获取到真实的IP地址. 但是生产上,这又是不允许的,那么怎么解决? 1.在NGINX反向代理服务器上进行修改 2.修改后端web服务器配置文件 ...

  8. 2015年工作中遇到的问题:81-90,标题党-Nginx与真实IP-Mybatis等

     81."标题党"与"百度收录"问题. 很久以来,就发现那些"标题党"的收录和排名情况非常好,比如CSDN某篇文章,就随便一写,就排在了第一 ...

  9. 后端Apache获取前端Nginx反向代理的真实IP地址 (原创贴-转载请注明出处)

    ====================说在前面的话==================== 环境:前段Nginx是反向代理服务器:后端是Apache是WEB项目服务器 目的:让后端Apapche获取 ...

随机推荐

  1. 大数据- Hive

    构建在Hadoop之上的数据仓库,数据计算使用MR,数据存储使用HDFS         由于数据计算使用mapreduce.因此通经常使用于进行离线数据处理 Hive 定义了一种类 SQL 查询语言 ...

  2. color-在framwork中添加属性变量

    1.今天在修改framwork中的代码的时候,需要把自己在代码中写的一个#ffffff,变成在xml中引用的变量.具体操作方法如下 1)在 frameworks/base/core/res/res/v ...

  3. 00103_死锁、Lock接口、等待唤醒机制

    1.死锁 (1)同步锁使用的弊端:当线程任务中出现了多个同步(多个锁)时,如果同步中嵌套了其他的同步.这时容易引发一种现象:程序出现无限等待,这种现象我们称为死锁.这种情况能避免就避免掉: synch ...

  4. 【习题 3-8 UVA - 202】Repeating Decimals

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 余数出现循环节. 就代表出现了循环小数. [代码] #include <bits/stdc++.h> using nam ...

  5. android 指定时间加一个小时算法

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class My ...

  6. Docker基础(一)

    1.安装:安装教程很多,Ubuntu14.04安装比较简单docker[之前使用Ubuntu13.04结果安装了好久也没有安装好,后来就直接是14,04了] 2.docker是容器,那么什么是容器? ...

  7. C语言结构体的字节对齐原则

    为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这就需要各种类型数据 ...

  8. windows下, nginx 提示错误 "No input file specified"

    https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80386035

  9. [PReact] Integrate Redux with Preact

    Redux is one of the most popular state-management libraries and although not specific to React, it i ...

  10. 不可摸数 【杭电-HDOJ-1999】 附题

    /* hdu 1999 不可摸数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...