基于nginx反向代理如何获取用户真实Ip地址
发表于:2025-11-09 作者:千家信息网编辑
千家信息网最后更新 2025年11月09日,小编给大家分享一下基于nginx反向代理如何获取用户真实Ip地址,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!引言ngin
千家信息网最后更新 2025年11月09日基于nginx反向代理如何获取用户真实Ip地址
小编给大家分享一下基于nginx反向代理如何获取用户真实Ip地址,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
引言
nginx做反向代理时,默认的配置后端获取到的Ip地址都来自于nginx,用request.getRemoteAddr();获取到的是nginx的ip地址,而不是用户的真实ip.
1.修改Nginx配置:
server { listen 80; server_name jenkins.local.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://192.168.10.204:8899; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; index index.html index.htm index.jsp index.action default.html; } proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }在原来的基础配置上加上后三行配置,就可以使用request.getHeader("x-forwarded-for")来获取用户真实的Ip地址了
2.java获取客户端Ip
package com.zimax.cqyf.admin.util;import javax.servlet.http.HttpServletRequest;import java.net.InetAddress;import java.net.UnknownHostException; /** * http工具类 */public class HttpUtils { /** * 获取真实的ip * @param request * @return * @throws UnknownHostException */ public static String getRealIp(HttpServletRequest request){ String ip; // 有的user可能使用代理,为处理用户使用代理的情况,使用x-forwarded-for if (request.getHeader("x-forwarded-for") == null) { ip = request.getRemoteAddr(); } else { ip = request.getHeader("x-forwarded-for"); } if ("127.0.0.1".equals(ip)) { try { // 获取本机真正的ip地址 ip = InetAddress.getLocalHost().getHostAddress(); }catch (Exception e){ e.printStackTrace(); } } return ip; } }附:一个ip工具类
import javax.servlet.http.HttpServletRequest;/*** IP地址工具类* @author xudongdong**/public class IpUtil { /** * 私有化构造器 */ private IpUtil() { } /** * 获取真实IP地址 * 使用getRealIP代替该方法
* @param request req * @return ip */ @Deprecated public static String getClinetIpByReq(HttpServletRequest request) { // 获取客户端ip地址 String clientIp = request.getHeader("x-forwarded-for"); if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) { clientIp = request.getHeader("Proxy-Client-IP"); } if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) { clientIp = request.getHeader("WL-Proxy-Client-IP"); } if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) { clientIp = request.getRemoteAddr(); } /* * 对于获取到多ip的情况下,找到公网ip. */ String sIP = null; if (clientIp != null && !clientIp.contains("unknown") && clientIp.indexOf(",") > 0) { String[] ipsz = clientIp.split(","); for (String anIpsz : ipsz) { if (!isInnerIP(anIpsz.trim())) { sIP = anIpsz.trim(); break; } } /* * 如果多ip都是内网ip,则取第一个ip. */ if (null == sIP) { sIP = ipsz[0].trim(); } clientIp = sIP; } if (clientIp != null && clientIp.contains("unknown")){ clientIp =clientIp.replaceAll("unknown,", ""); clientIp = clientIp.trim(); } if ("".equals(clientIp) || null == clientIp){ clientIp = "127.0.0.1"; } return clientIp; } /** * 判断IP是否是内网地址 * @param ipAddress ip地址 * @return 是否是内网地址 */ public static boolean isInnerIP(String ipAddress) { boolean isInnerIp; long ipNum = getIpNum(ipAddress); /** 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址 **/ long aBegin = getIpNum("10.0.0.0"); long aEnd = getIpNum("10.255.255.255"); long bBegin = getIpNum("172.16.0.0"); long bEnd = getIpNum("172.31.255.255"); long cBegin = getIpNum("192.168.0.0"); long cEnd = getIpNum("192.168.255.255"); isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || ipAddress.equals("127.0.0.1"); return isInnerIp; } private static long getIpNum(String ipAddress) { String[] ip = ipAddress.split("\\."); long a = Integer.parseInt(ip[0]); long b = Integer.parseInt(ip[1]); long c = Integer.parseInt(ip[2]); long d = Integer.parseInt(ip[3]); return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d; } private static boolean isInner(long userIp, long begin, long end) { return (userIp >= begin) && (userIp <= end); } public static String getRealIP(HttpServletRequest request){ // 获取客户端ip地址 String clientIp = request.getHeader("x-forwarded-for"); if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) { clientIp = request.getRemoteAddr(); } String[] clientIps = clientIp.split(","); if(clientIps.length <= 1) return clientIp.trim(); // 判断是否来自CDN if(isComefromCDN(request)){ if(clientIps.length>=2) return clientIps[clientIps.length-2].trim(); } return clientIps[clientIps.length-1].trim(); } private static boolean isComefromCDN(HttpServletRequest request) { String host = request.getHeader("host"); return host.contains("www.189.cn") ||host.contains("shouji.189.cn") || host.contains( "image2.chinatelecom-ec.com") || host.contains( "image1.chinatelecom-ec.com"); }}以上是"基于nginx反向代理如何获取用户真实Ip地址"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
地址
用户
代理
配置
客户
客户端
工具
篇文章
内容
情况
私有
不怎么
基础
大部分
引言
方法
更多
知识
网段
行业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广东公安厅网络安全宣传
软件开发有竞赛没
网络技术三级各种英文概念
大兴区定制软件开发特点
天津软件开发王涛
北京昊识网络技术
楚雄上门回收服务器
网络安全主要是包含什么
洛阳软件开发哪个培训班好
网络安全模式连不上网络
通用网络技术销售公司
防止服务器崩掉的东西
网络安全认证的流程
电信网络安全工程工作职责
2021网络安全宣传周直播回放
易语言 mdb数据库
p2p平台软件开发商
数据库技术考卷
正规软件开发厂家
常州软件开发五星服务
沈阳网络安全报案
数据库规范化解决问题
其他软件开发工具
关于网络安全的手工帽
网络安全美术字体
高新网络技术
学生网络安全讲话稿
tcp是第几代计算机网络技术
华鑫互联网科技
网络安全的相关对策有哪些