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
Table of contents 1. Master-slave synchronization...
I have always been interested in wireless interac...
Contents of this article: Page hollow mask layer,...
1 Introduction Thread technology was proposed as ...
Method 1: hostnamectl modification Step 1 Check t...
Introduction to Nginx Nginx is a high-performance...
1. Introduction MDL lock in MYSQL has always been...
Mysql left join is invalid and how to use it When...
In actual web page development, accordions also a...
Table of contents 1. Log related services 2. Comm...
A mature database architecture is not designed wi...
Table of contents 1. Problems encountered 2. Idea...
Preface: Use the element framework in vue3.0, bec...
In CSS3, the transform function can be used to im...
Table of contents Preface Input box component lay...