Obtain the real IP address of the client under multi-level reverse proxy [Squid]

Source: Internet
Author: User

In many applications, you may need to record the real IP address of the user. In this case, you need to obtain the real IP address of the user. In JSP, you can obtain the IP address of the client by: request. getRemoteAddr (), which is effective in most cases. However, the real IP address of the client cannot be obtained through reverse proxy software such as Apache and Squid.

During this time, the IP address statistics program is designed. Because the server is a cluster, the reverse proxy software is used to reverse proxy the URL of http: // 192.168.1.110: 2046/to URL at http://www.xxx.com/, and request.getremoteaddr () the obtained IP address is 127.0.0.1 or 192.168.1.110, but not the real IP address of the client. Why?

This is the reason for reverse proxy. After proxy, because the intermediate layer is added between the client and the service, the server cannot directly obtain the IP address of the client, and the server application cannot directly return the IP address of the forwarded request to the client. However, X-FORWARDED-FOR information is added in the HTTP header information that forwards the request. It is used to track the original Client IP address and the server address of the original client request. When we access the forward () method, the obtained IP address is actually the proxy server address, not the client IP address.

So we can obtain the real IP address of the client. Method 1:

Public String getIpAddr (HttpServletRequest request ){
String ip = request. getHeader ("x-forwarded-");
If (ip = null | ip. length () = 0 ){
Ip = request. getRemoteAddr ();
}
Return ip;
} However, when I access http://www.xxx.com/index.jsp/, the IP address returned is always unknown, and it is not the 127.0.0.1 or 192.168.1.110 shown in the preceding figure. However, I access http: // 192.168.1.110: 2046/index. jsp, the real IP address of the client can be returned, and a method is written for verification.

<% @ Page import = "java. util. *" %>
<Table border = 1 cellspacing = 0 cellpadding = 0 align = center>
<Tr>
<Th> Name </th>
<Th> Value </th>
</Tr>
<%
Enumeration enumNames;
String strName, strValue;

EnumNames = request. getHeaderNames ();
While (enumNames. hasMoreElements ()){
StrName = (String) enumNames. nextElement ();
StrValue = request. getHeader (strName );
%>
<Tr>
<Td> <% = strName %> </td>
<Td> <% = strValue %> </td>
</Tr>
<%
}
%>
<Tr>
</Table>


Nginx + (1... N) when the tomcat cluster is deployed, the backend tomcat needs to obtain the user's IP address. At this time, the result obtained through request. getRemoteAddr () is always 127.0.0.1.
After a user request passes through nginx and ngigx forwards the request to tomcat, tomcat always obtains the nginx Ip address. To obtain the real user IP address, you only need to configure it on ngix.
Location/lower settings
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header Host $ host;

Test Configuration:./sbin/nginx-t-c conf/nginx. conf is correct and then restart nginx.
The Java code is modified as follows:
Out. println ("X-Real-IP:" + request. getHeader ("X-Real-IP") +"
");
X-Real-IP is the Real IP address of the user.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.