Nginx与真实IP
来源:互联网 发布:网络公开课观后感 编辑:程序博客网 时间:2024/05/01 23:58
获得远程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
- nginx的真实IP
- nginx 获取真实IP
- Nginx之反向代理与真实ip地址丢失-yellowcong
- nginx中获取真实ip
- nginx中获取真实ip
- Nginx获取真实用户IP
- nginx中获取真实ip
- nginx lua获取真实IP
- lighttpd获取nginx代理客户端真实IP
- nginx+tomcat整合后获取真实IP
- WordPress+Nginx+proxy取得真实IP
- nginx转发apache显示真实IP
- Nginx 教程- 获取真实IP模块 - http_realip_module
- WordPress+Nginx+proxy取得真实IP
- nginx反向代理,node获取真实IP
- Nginx反向代理获取客户端真实IP
- nginx 反向代理取真实IP地址
- CocoaPods详解之----使用篇
- 白话机器学习算法(十三)LVQ
- leetcode_24_Swap Nodes in Pairs
- Ubuntu 12.04启动错误:Checking Battery State ...
- Android Animation学习笔记
- Nginx与真实IP
- Android任务和返回栈完全解析
- 关于数据库存储的问题
- Lance老师UI系列教程第九课->高仿比特币监控大师
- Ubuntu 12.04启动错误:saned disabled;edit /etc/default/saned
- 白话机器学习算法(十四)数据降维
- HTML 中表格table 的相关知识
- MongoDB 聚合管道(一)(Aggregation Pipeline)
- 与状态模式相似的设计模式---职责链模式