Solve the problem that the IP address obtained using nginx is 127.0.0.1

Solve the problem that the IP address obtained using nginx is 127.0.0.1

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:
  • How to use Nginx to prevent IP addresses from being maliciously resolved
  • Example of configuring multiple SSL certificates for a single Nginx IP address

<<:  A detailed explanation of the subtle differences between Readonly and Disabled

>>:  MySQL not null constraint case explanation

Recommend

MySQL master-slave synchronization principle and application

Table of contents 1. Master-slave synchronization...

Example code for implementing a hollow mask layer with CSS

Contents of this article: Page hollow mask layer,...

Analysis of multi-threaded programming examples under Linux

1 Introduction Thread technology was proposed as ...

Three methods to modify the hostname of Centos7

Method 1: hostnamectl modification Step 1 Check t...

Nginx signal control

Introduction to Nginx Nginx is a high-performance...

MYSQL METADATA LOCK (MDL LOCK) MDL lock problem analysis

1. Introduction MDL lock in MYSQL has always been...

Native js to achieve accordion effect

In actual web page development, accordions also a...

Detailed introduction to logs in Linux system

Table of contents 1. Log related services 2. Comm...

A brief introduction to MySQL database optimization techniques

A mature database architecture is not designed wi...

Complete steps to use element in vue3.0

Preface: Use the element framework in vue3.0, bec...

4 functions implemented by the transform attribute in CSS3

In CSS3, the transform function can be used to im...

How to elegantly implement the mobile login and registration module in vue3

Table of contents Preface Input box component lay...