Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to obtain the user's real Ip address based on nginx reverse proxy

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you based on the nginx reverse agent how to obtain the real Ip address of users, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Introduction

When nginx acts as a reverse proxy, the default configuration backend gets the Ip address from nginx, using request.getRemoteAddr (), and gets the ip address of nginx, not the real ip of the user.

1. Modify Nginx configuration: 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 500502 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;}

Add the last three lines to the original basic configuration, and you can use request.getHeader ("x-forwarded-for") to obtain the user's real Ip address.

2.java get client Ippackage com.zimax.cqyf.admin.util;import javax.servlet.http.HttpServletRequest;import java.net.InetAddress;import java.net.UnknownHostException; / * http tool class * / public class HttpUtils {/ * get the real ip * @ param request * @ return * @ throws UnknownHostException * / public static String getRealIp (HttpServletRequest request) {String ip / / some user may use agents. To handle the situation where users use agents, use 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 {/ / get the local real ip address ip = InetAddress.getLocalHost () .getHostAddress ();} catch (Exception e) {e.printStackTrace ();}} return ip }} attached: an ip tool class import javax.servlet.http.HttpServletRequest;/*** IP address tool class * @ author xudongdong**/public class IpUtil {/ * * privatization constructor * / private IpUtil () {} / * * get the real IP address *

Use getRealIP instead of this method

* @ param request req * @ return ip * / @ Deprecated public static String getClinetIpByReq (HttpServletRequest request) {/ / obtain client ip address 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 () } / * * if multiple ip is obtained, find the public network 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;}} / * * if all ip are private network ip, take the first 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;} / * determine whether IP is an intranet address * @ param ipAddress ip address * @ return is an intranet address * / public static boolean isInnerIP (String ipAddress) {boolean isInnerIp Long ipNum = getIpNum (ipAddress) / * * Private IP:A class 10.0.0.0-10.255.255.255 Class B 172.16.0.0-172.31.255.255 Class C 192.168.0.0-192.168.255.255 of course The other 127is the loopback address * * / 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 * 256256 + b * 256256 + c * 256d;} private static boolean isInner (long userIp, long begin, long end) {return (userIp > = begin) & & (userIp

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report