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

How to install iso file in Linux system

How to install iso files under Linux system? Inst...

Usage of the target attribute of the html tag a

1: If you use the tag <a> to link to a page,...

Remote development with VSCode and SSH

0. Why do we need remote development? When develo...

Analysis of the process of building a cluster environment with Apache and Tomcat

In fact, it is not difficult to build an Apache c...

An example of using CSS methodologies to achieve modularity

1. What are CSS methodologies? CSS methodologies ...

Implementation idea of ​​left alignment of the last row of flex box layout

Using flex layout, if it is a nine-square grid, i...

Some improvements in MySQL 8.0.24 Release Note

Table of contents 1. Connection Management 2. Imp...

CentOS8 network card configuration file

1. Introduction CentOS8 system update, the new ve...

A brief discussion of the interesting box model of CSS3 box-sizing property

Everyone must know the composition of the box mod...

When is it appropriate to use dl, dt, and dd?

dl:Definition list Definition List dt:Definition t...

A simple tutorial on how to use the mysql log system

Table of contents Preface 1. Error log 2. Binary ...

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...