Get ip tool import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.servlet.http.HttpServletRequest; /** * IP address * * @date March 6, 2020 12:57:02 PM */ @Slf4j public class IPUtils { /** * Get IP address * * If you use reverse proxy software such as Nginx, you cannot get the IP address through request.getRemoteAddr()* If you use multiple levels of reverse proxy, the value of X-Forwarded-For is not just one, but a string of IP addresses. The first valid IP string that is not unknown in X-Forwarded-For is the real IP address*/ public static String getIpAddr(HttpServletRequest request) { String ip = null; try { ip = request.getHeader("x-forwarded-for"); if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } } catch (Exception e) { log.error("IPUtils ERROR ", e); } //Using a proxy, get the first IP address if(StringUtils.isEmpty(ip) && ip.length() > 15) { if(ip.indexOf(",") > 0) { ip = ip.substring(0, ip.indexOf(",")); } } return ip; } } If you use nginx, the IP you get will be 127.0.0.1 Add the following configuration to the proxy: proxy_set_header x-forwarded-for $remote_addr; server { listen 80; server_name api.qimen.pro; # Server file upload size limit client_max_body_size 10M; location / { proxy_pass http://gymserver; proxy_set_header x-forwarded-for $remote_addr; } } This is the end of this article about solving the problem of using nginx to obtain the IP address of 127.0.0.1. For more information about the problem of nginx obtaining the IP address, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A detailed explanation of the subtle differences between Readonly and Disabled
>>: MySQL not null constraint case explanation
How to install iso files under Linux system? Inst...
In Linux systems, especially server systems, it i...
1: If you use the tag <a> to link to a page,...
0. Why do we need remote development? When develo...
Table of contents Preface 1. Reasons: 2. Solution...
Three functions: 1. Automatic vertical centering o...
In fact, it is not difficult to build an Apache c...
1. What are CSS methodologies? CSS methodologies ...
Using flex layout, if it is a nine-square grid, i...
Table of contents 1. Connection Management 2. Imp...
1. Introduction CentOS8 system update, the new ve...
Everyone must know the composition of the box mod...
dl:Definition list Definition List dt:Definition t...
Table of contents Preface 1. Error log 2. Binary ...
environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...