> For the complete documentation index, see [llms.txt](https://docshome.gitbook.io/nginx-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_jwt_module.md).

# ngx\_http\_auth\_jwt\_module

* [示例配置](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_jwt_module.md#example_configuration)
* [指令](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_jwt_module.md#directives)
  * [auth\_jwt](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_jwt_module.md#auth_jwt)
  * [auth\_jwt\_header\_set](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_jwt_module.md#auth_jwt_header_set)
  * [auth\_jwt\_claim\_set](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_jwt_module.md#auth_jwt_claim_set)
  * [auth\_jwt\_key\_file](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_jwt_module.md#auth_jwt_key_file)
* [内嵌变量](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_jwt_module.md#embedded_variables)

`ngx_http_auth_jwt_module` 模块（1.11.3）通过验证使用指定的密钥提供的 [JSON Web Token](https://tools.ietf.org/html/rfc7519)（JWT）来实现客户端授权。JWT claims 必须以 [JSON Web Signature](https://tools.ietf.org/html/rfc7515)（JWS）结构编码。该模块可用于 [OpenID Connect](http://openid.net/specs/openid-connect-core-1_0.html) 身份验证。

该模块可以通过 [satisfy](/nginx-docs/he-xin-gong-neng/http/ngx_http_core_module.md#satisfy) 指令与其他访问模块（如 [ngx\_http\_access\_module](/nginx-docs/he-xin-gong-neng/http/ngx_http_access_module.md)、[ngx\_http\_auth\_basic\_module](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_basic_module.md) 和 [ngx\_http\_auth\_request\_module](/nginx-docs/he-xin-gong-neng/http/ngx_http_auth_request_module.md)）进行组合。

> 此模块可作为我们商业订阅的一部分。

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

```
location / {
    auth_jwt          "closed site";
    auth_jwt_key_file conf/keys.json;
}
```

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

### auth\_jwt

| -       | 说明                                                 |
| ------- | -------------------------------------------------- |
| **语法**  | **auth\_jwt** `string [token=$variable]` \| `off`; |
| **默认**  | auth\_jwt off;                                     |
| **上下文** | http、server、location                               |

启用 JSON Web Token 验证。指定的字符串作为一个 `realm`。参数值可以包含变量。

可选的 `token` 参数指定一个包含 JSON Web Token 的变量。默认情况下，JWT 作 [Bearer Token](https://tools.ietf.org/html/rfc6750) 在 **Authorization** 头中传递。JWT 也可以作为 cookie 或查询字符串的一部分传递：

```
auth_jwt "closed site" token=$cookie_auth_token;
```

特殊值 `off` 取消从上一配置级别继承的 `auth_jwt` 指令的作用。

### auth\_basic\_user\_file

| -       | 说明                                           |
| ------- | -------------------------------------------- |
| **语法**  | **auth\_jwt\_header\_set** `$variable name`; |
| **默认**  | ——                                           |
| **上下文** | http                                         |
| **提示**  | 该指令在 1.11.10 版本中出现                           |

将 `variable` 设置为给定的 JOSE 头参数 `name`。

### auth\_jwt\_claim\_set

| -       | 说明                                          |
| ------- | ------------------------------------------- |
| **语法**  | **auth\_jwt\_claim\_set** `$variable name`; |
| **默认**  | ——                                          |
| **上下文** | http                                        |
| **提示**  | 该指令在 1.11.10 版本中出现                          |

将 `variable` 设置为给定的 JWT claim 参数 `name`。

### auth\_jwt\_key\_file

| -       | 说明                               |
| ------- | -------------------------------- |
| **语法**  | **auth\_jwt\_key\_file** `file`; |
| **默认**  | ——                               |
| **上下文** | http、server、location             |

指定用于验证 JWT 签名的 [JSON Web Key Set](https://tools.ietf.org/html/rfc7517#section-5) 格式的 `file`（文件）。参数值可以包含变量。

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

`ngx_http_auth_jwt_module` 模块支持内嵌变量：

* `$jwt_header_name`

  返回 [JOSE 头](https://tools.ietf.org/html/rfc7515#section-4)的值
* `$jwt_claim_name`

  返回 [JWT claim](https://tools.ietf.org/html/rfc7519#section-4) 的值

## 原文档

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