# ngx\_http\_referer\_module

* [示例配置](/nginx-docs/he-xin-gong-neng/http/ngx_http_referer_module.md#example_configuration)
* [指令](/nginx-docs/he-xin-gong-neng/http/ngx_http_referer_module.md#directives)
  * [referer\_hash\_bucket\_size](/nginx-docs/he-xin-gong-neng/http/ngx_http_referer_module.md#referer_hash_bucket_size)
  * [referer\_hash\_max\_size](/nginx-docs/he-xin-gong-neng/http/ngx_http_referer_module.md#referer_hash_max_size)
  * [valid\_referers](/nginx-docs/he-xin-gong-neng/http/ngx_http_referer_module.md#valid_referers)
* [内嵌变量](/nginx-docs/he-xin-gong-neng/http/ngx_http_referer_module.md#embedded_variables)

`ngx_http_referer_module` 模块用于阻止 **Referer** 头字段为无效值的请求访问站点。需记住的是，使用适当的 **Referer** 字段值来伪造请求非常容易，因此本模块的预期目的不是要彻底阻止此类请求，而是阻止常规浏览器发送的大量流量请求。还应该考虑到，即使是有效请求，常规浏览器也可能不发送 **Referer** 字段。

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

```
valid_referers none blocked server_names
               *.example.com example.* www.example.org/galleries/
               ~\.google\.;

if ($invalid_referer) {
    return 403;
}
```

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

### referer\_hash\_bucket\_size

| -       | 说明                                      |
| ------- | --------------------------------------- |
| **语法**  | **referer\_hash\_bucket\_size** `size`; |
| **默认**  | referer\_hash\_bucket\_size 64;         |
| **上下文** | server、location                         |

设置有效引用哈希表的桶大小。设置哈希表的详细信息在单独的[文档](/nginx-docs/readme/she-zhi-ha-xi.md)中提供。

### referer\_hash\_max\_size

| -       | 说明                                   |
| ------- | ------------------------------------ |
| **语法**  | **referer\_hash\_max\_size** `size`; |
| **默认**  | referer\_hash\_max\_size 2048;       |
| **上下文** | server、location                      |
| **提示**  | 该指令在 1.0.5 版本中出现                     |

设置有效引用哈希表的最大 `size`。设置哈希表的详细信息在单独的[文档](/nginx-docs/readme/she-zhi-ha-xi.md)中提供。

### valid\_referers

| -       | 说明                                                                         |
| ------- | -------------------------------------------------------------------------- |
| **语法**  | **valid\_referers** `none` \| `blocked` \| `server_names` \| `string ...`; |
| **默认**  | ——                                                                         |
| **上下文** | server、location                                                            |

指定 **Referer** 请求头字段值将导致内嵌的 `$invalid_referer` 变量设置为空字符串。否则，变量将为 `1`。匹配搜索不区分大小写。

参数说明如下：

* `none`

  请求头中缺少 **Referer** 字段
* `blocked`

  **Referer** 字段出现在请求头中，但其值已被防火墙或代理服务器删除，这些值为不以 `http://` 或 `https://` 开头的字符串
* `server_names`

  **Referer** 请求头字段包含一个服务器名称
* 任意字符串

  定义一个服务器名称和一个可选的 URI 前缀。服务器名称的开头或结尾可以包含`*`。在检查期间，**Referer** 字段中的服务器端口被忽略
* 正则表达式

  第一个符号应为 `〜`。要注意的是，表达式只与 `http://` 或 `https://` 之后的文本匹配。

示例：

```
valid_referers none blocked server_names
               *.example.com example.* www.example.org/galleries/
               ~\.google\.;
```

## 内嵌变量 <a href="#embedded_variables" id="embedded_variables"></a>

* `$invalid_referer`

  如果 **Referer** 请求头字段的值[有效](/nginx-docs/he-xin-gong-neng/http/ngx_http_referer_module.md#valid_referers)，则为空字符串，否则为 1。

## 原文档

<http://nginx.org/en/docs/http/ngx_http_referer_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_referer_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.
