# ngx\_http\_geo\_module

* [示例配置](/nginx-docs/he-xin-gong-neng/http/ngx_http_geo_module.md#example_configuration)
* [指令](/nginx-docs/he-xin-gong-neng/http/ngx_http_geo_module.md#directives)
  * [geo](/nginx-docs/he-xin-gong-neng/http/ngx_http_geo_module.md#geo)

`ngx_http_geo_module` 模块创建带值变量需依赖客户端 IP 地址。

## 示例配置 <a href="#example_configuration" id="example_configuration"></a>

```
geo $geo {
    default        0;

    127.0.0.1      2;
    192.168.1.0/24 1;
    10.1.0.0/16    1;

    ::1            2;
    2001:0db8::/32 1;
}
```

## 指令 <a href="#directives" id="directives"></a>

### geo

|       - | 说明                                      |
| ------: | --------------------------------------- |
|  **语法** | **geo** `[$address] $variable { ... }`; |
|  **默认** | ——                                      |
| **上下文** | http                                    |

描述指定变量的值对客户端 IP 地址的依赖。默认情况下，地址来自 `$remote_addr` 变量，但也可以从另一个变量（0.7.27）中获取，例如：

```
geo $arg_remote_addr $geo {
    ...;
}
```

> 由于变量仅在使用时才生效，因此即使存在大量已声明的 `geo` 变量也不会增加请求处理开销。

如果变量的值不是有效的 IP 地址，则使用 `255.255.255.255` 地址。

> 从1.3.10 版本和 1.2.7 版本开始支持 IPv6 前缀。

也支持以下特殊参数：

* `delete`

  删除指定的网络（0.7.23）。
* `default`

  如果客户端地址与所有指定地址都不匹配，则将设置为该变量的值。当以 CIDR 表示法指定地址时，可以使用 `0.0.0.0/0` 和 `::/0` 代替默认值。未指定默认值时，默认值为空字符串。
* `include`

  包含一个包含地址和值的文件。可包含多个。
* `proxy`

  定义可信地址（0.8.7、0.7.63）。当请求来自可信地址时，将使用来自 `X-Forwarded-For` 请求头字段的地址。与常规地址相比，可信地址是按顺序检查的。

  > 从 1.3.0 版本和 1.2.1 版本开始支持 IPv6 地址。
* `proxy_recursive`

  启用递归地址搜索（1.3.0、1.2.1）。如果递归搜索被禁用，将使用在 `X-Forwarded-For` 中发送的最后一个地址，而不是匹配其中一个可信地址的原始客户端地址。如果启用递归搜索，则将使用在 `X-Forwarded-For` 中发送的最后一个不可信地址，而不是匹配其中一个可信地址的原始客户端地址。
* `ranges`

  表示地址被指定为范围形式（0.7.23）。这个参数应该放在首位。想要加快加载地理区域，地址应按升序排列。

示例：

```
geo $country {
    default        ZZ;
    include        conf/geo.conf;
    delete         127.0.0.0/16;
    proxy          192.168.100.0/24;
    proxy          2001:0db8::/32;

    127.0.0.0/24   US;
    127.0.0.1/32   RU;
    10.1.0.0/16    RU;
    192.168.1.0/24 UK;
}
```

`conf/geo.conf` 文件可能包含以下内容：

```
10.2.0.0/16    RU;
192.168.2.0/24 RU;
```

使用最明确匹配的值。例如，对于 127.0.0.1 地址，将选择 `RU` 值，而不是 `US`。

范围示例：

```
geo $country {
    ranges;
    default                   ZZ;
    127.0.0.0-127.0.0.0       US;
    127.0.0.1-127.0.0.1       RU;
    127.0.0.1-127.0.0.255     US;
    10.1.0.0-10.1.255.255     RU;
    192.168.1.0-192.168.1.255 UK;
}
```

## 原文档

<http://nginx.org/en/docs/http/ngx_http_geo_module.html>


---

# Agent Instructions: 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://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/http/ngx_http_geo_module.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.
