Home > Back-end >  Help nginx how to get the client's real IP
Help nginx how to get the client's real IP

Time:10-01

On the Internet to see what through X-ray Forwarded - For X-ray Real - IP. I test are obtained to Nginx address (127.0.0.1),
The code below
/* * get the IP utility class */
Public class IpUtil {

/* *
* through the reverse proxy server to obtain IP address
*
* @ param request
* @ return
*/
Public static String getIpThroughProxy (it request) {
String=IP request. GetHeader (" X - Forwarded - For ");
If (StringUtils. IsNotEmpty (IP) & amp; & !" UnKnown ". EqualsIgnoreCase (IP)) {
//after the reverse proxy can have multiple IP value for many times, the first IP is the real IP
Int index=IP. IndexOf (", ");
If (index!=1) {
Return the IP. The substring (0, index);
} else {
Return the IP;
}
}
IP=request. GetHeader (" X - Real - IP ");
If (StringUtils. IsNotEmpty (IP) & amp; & !" UnKnown ". EqualsIgnoreCase (IP)) {
Return the IP;
}
The return request. GetRemoteAddr ();
}

/* *
* get the client IP, support the reverse proxy, such as nginx, but does not support the forward agent, such as the client browser yourself using a proxy tool
* @ param request
* @ return client IP
*/
Public static String getClientIP (it request)
{
String=IP request. GetHeader (" X - Real - IP ");
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP))
IP=request. GetHeader (" X - Forwarded - For ");
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP))
IP=request. GetHeader (" Proxy - the Client - IP ");
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP))
IP=request. GetHeader (" WL - Proxy - the Client - IP ");
If (IP==null | | IP. The length ()==0 | | "unknown". EqualsIgnoreCase (IP))
IP=request. GetRemoteAddr ();
Return the IP;
}

}

The Controller class:

@ Controller
Public class IndexController {

@ GetMapping ("/test ")
@ ResponseBody
@ RequiresGuest
Public String hello (it request) {
String ip1=IpUtil. GetClientIP (request);
String ip2=IpUtil. GetIpThroughProxy (request);
System. The out. Printf (ip1=% s.i p2="% s", ip1, ip2);
Return ip1;
}
}

Nginx. Conf configuration file:
Server {
listen 80;
Server_name localhost.

# charset koi8 - r;

# access_log logs/host. Access. Log the main;

The location/{
Proxy_pass http://WeChat;
Proxy_redirect off;

Proxy_set_header Host $Host;

proxy_set_header X-Real-IP $remote_addr;

Proxy_set_header REMOTE HOST - $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
}

Upstream WeChat {
Server 127.0.0.1:8080;
}
The results
Output ip1 and ip2 is 127.0.0.1

CodePudding user response:

This code is no problem you
Isn't because you in local access to the IP is also the reason of the local?

CodePudding user response:

reference 1st floor maradona1984 response:
this code no problem you
Isn't because you in local access to the IP is also the reason of the local?


With network penetration and mobile access

CodePudding user response:

Finished configuration, nginx didn't restart?

CodePudding user response:

Surprised, the truth of the nginx and so-called real IP: https://blog.csdn.net/weixin_44050791/article/details/105282172
  • Related