> For the complete documentation index, see [llms.txt](https://jupiter-1992.gitbook.io/jupiter-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jupiter-1992.gitbook.io/jupiter-note/hou-duan/shi-zhan-xi-lie/03-huo-qu-qing-qiu-ip-di-zhi.md).

# 03 获取请求 IP 地址

在 Spring 中，获取客户端真实 IP 地址的方法是 `request.getRemoteAddr()`，这种方法在大部分情况下都是有效的，但是在通过了 Squid 等反向代理软件就无法工作。

如果使用了反向代理软件，将 `http://192.168.1.110:2046/` 的 URL 反向代理为 `http://www.abc.com/` 的 URL 时，用`request.getRemoteAddr()` 方法获取的 IP 地址是 127.0.0.1 或 192.168.1.110，而并不是客户端的真实 IP。

经过代理以后，由于在客户端和服务之间增加了中间层，因此服务器无法直接拿到客户端的 IP，服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的 HTTP 头信息中，增加了 `X-FORWARDED-FOR` 信息，用以跟踪原有的客户端 IP 地址和原来客户端请求的服务器地址。

当我们访问 `http://www.abc.com` 时，其实并不是我们浏览器真正访问到了服务器上，而是先由代理服务器去访问 `http://192.168.1.110:2046`，代理服务器再将访问到的结果返回给我们的浏览器，因为是代理服务器去访问真实服务器，所以通过 `request.getRemoteAddr()` 的方法获取的 IP 实际上是代理服务器的地址，并不是客户端的 IP 地址。

下面是一种在 Java 服务器中获取请求 ip 的常见方式：

```java
package com.titan.toolcenter.utils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author SHIYU
 * @date 2019/12/23 9:02
 * @description 获取请求真实IP
 */
public class IpUtil {

    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP，多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }
}
```

食用方式：

```java
@RestController
@RequestMapping("/pay")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "支付管理", tags = {"支付管理"})
public class PayOrderController {

    /**
     * 头部信息
     */
    private final HttpServletRequest servletRequest;

     /**
     * 支付订单
     */
    @PostMapping("/order")
    @ApiOperation(value = "订单详情", notes = "订单详情")
    public CommJSONResult order(@ApiParam(value = "参数", required = true) @RequestBody PayOrderParams bean) throws Exception {
        String ip = IpUtil.getIpAddr(servletRequest);
    }

}
```

这里解释下这些请求头的意思：

**X-Forwarded-For**

这是一个 Squid 开发的字段，只有在通过了 HTTP 代理或者负载均衡服务器时才会添加该项。

格式为 `X-Forwarded-For:client1,proxy1,proxy2`，一般情况下，第一个 ip 为客户端真实 ip，后面的为经过的代理服务器 ip。现在大部分的代理都会加上这个请求头。

**Proxy-Client-IP/WL- Proxy-Client-IP**

这个一般是经过 apache http 服务器的请求才会有，用 apache http 做代理时一般会加上 `Proxy-Client-IP` 请求头，而 `WL-Proxy-Client-IP` 是他的 weblogic 插件加上的请求头。

需要注意几点：

* 这些请求头都不是 http 协议里的标准请求头，也就是说这是各个代理服务器自己规定的表示客户端地址的请求头。如果哪天有一个代理服务器软件用 `xxx-client-ip` 这个请求头代表客户端请求，那上面的代码就不行了。
* 这些请求头不是代理服务器一定会带上的，网络上的很多匿名代理就没有这些请求头，所以获取到的客户端 ip 不一定是真实的客户端 ip。代理服务器一般都可以自定义请求头设置。
* 即使请求经过的代理都会按自己的规范附上代理请求头，上面的代码也不能确保获得的一定是客户端 ip。不同的网络架构，判断请求头的顺序是不一样的。
* 最重要的一点，请求头都是可以伪造的。如果一些对客户端校验较严格的应用（比如投票）要获取客户端 ip，应该直接使用 `request.getRemoteAddr()`，虽然获取到的可能是代理的 ip 而不是客户端的 ip，但这个获取到的 ip 基本上是不可能伪造的，也就杜绝了刷票的可能。

## nginx 配置

```
server {
    listen 80;
    server_name liv6565.com;

    location / {
        proxy_connect_timeout 300s;
        proxy_send_timeout   300s;
        proxy_read_timeout   300s;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://api:8080;
        proxy_redirect http:// https://;
        client_max_body_size 300M;
    }

    location /chat {
        access_log off;
        proxy_pass http://api:7002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 1d;
        proxy_send_timeout 1d;
        proxy_read_timeout 1d;
    }

}
```

`$proxy_add_x_forwarded_for` 与 `$http_x_forwarded_for` 这两个的变量的值的区别就在于 `$proxy_add_x_forwarded_for` 比`$http_x_forwarded_for` 多了一个 `$remote_addr` 的值。

`$remote_addr` 只能获取到与服务器本身直连的上层请求 ip，所以设置 `$remote_addr` 一般都是设置第一个代理上面。如果用户通过 cdn 访问过来的，那么后面 web 服务器获取到的，永远都是 cdn 的 ip 而非真是用户 ip，这时就要用到 x-forward—for 了，这个变量其实就像是链路反追踪，从客户的真实 ip 为起点，穿过多层级的 proxy，最终到达 web 服务器，都会记录下来。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jupiter-1992.gitbook.io/jupiter-note/hou-duan/shi-zhan-xi-lie/03-huo-qu-qing-qiu-ip-di-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
