跳转至

pta: NGINX的时间段认证模块

安装

您可以在任何基于RHEL的发行版中安装此模块,包括但不限于:

  • RedHat Enterprise Linux 7, 8, 9 和 10
  • CentOS 7, 8, 9
  • AlmaLinux 8, 9
  • Rocky Linux 8, 9
  • Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-pta
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-pta

通过在 /etc/nginx/nginx.conf 的顶部添加以下内容来启用模块:

load_module modules/ngx_http_pta_module.so;

本文档描述了 nginx-module-pta v1.0.2,于2025年1月7日发布。


概述

PTA(时间段认证)模块是一个用于NGINX的模块。使用 PTA,您可以控制对内容的访问。PTA 计算一个包含过期时间和内容路径的加密查询字符串或 cookie 参数。

用法

以下是 nginx.conf 的示例。

  worker_processes  1;

  events {
      worker_connections  1024;
  }


  http {
      include       mime.types;
      default_type  application/octet-stream;

      sendfile        on;

      keepalive_timeout  65;

      server {
          listen       80;
          server_name  localhost;

          pta_1st_key 0102030405060708090a0b0c0d0e0f00;
          pta_1st_iv  00000000000000000000000000000000;
          pta_2nd_key 11111111111111111111111111111111;
          pta_2nd_iv  22222222222222222222222222222222;

          location / {
              root   html;
              index  index.html index.htm;
          }

          location /foo/ {
              pta_enable on;
          }

          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   html;
          }
      }
  }

pta_1st_key

  • 语法 : pta_1st_key keystring
  • 默认值 : -
  • 上下文 : server

pta_1st_iv

  • 语法 : pta_1st_iv ivstring;
  • 默认值 : -
  • 上下文 : server

pta_2nd_key

  • 语法 : pta_2nd_key keystring;
  • 默认值 : -
  • 上下文 : server

pta_2nd_iv

  • 语法 : pta_2nd_iv ivstring;
  • 默认值 : -
  • 上下文 : server

pta_enable

  • 语法 : pta_enable on | off;
  • 默认值 : pta_enable off;
  • 上下文 : location

pta_auth_method

  • 语法 : pta_auth_method qs | cookie | qs cookie;
  • 默认值 : pta_auth_method qs;
  • 上下文 : location

工作原理

PTA 模块解密以 `pta=...' 开头的查询字符串或 cookie 参数,并检查嵌入其中的 CRC32、过期时间和请求的 URI 路径。因此,您需要生成 PTA 令牌并将其作为查询字符串或 cookie 参数添加到链接中。在 samples 目录下有一些代码用于生成 PTA。

格式

此字节流使用 AES 128 位 CBC 模式进行加密。

  +---------------+-------------------------+----------+-----------------+
  | CRC32 (4byte) | Expiration Time (8byte) | URI Path | Padding         |
  |               | Unix Time format        |          | pkcs #7 format  |
  +---------------+-------------------------+----------+-----------------+

CRC32

它是大端格式。它是根据过期时间和 URI 路径计算的。此部分用于检查 AES 解密是否有效。

过期时间

它是大端格式。它与请求到达的时间进行比较,如果时间小于或等于 PTA 令牌中包含的过期时间,则请求被允许。

URI 路径

基本上,它必须与请求内容的路径相同。

例如: http://example.com/index.html -> /index.html

它必须以斜杠 / 开头。

星号字符 * 表示通配符。

  • \* 字符必须只有一个。 例如,/foo/*/bar/*.jpg 是不允许的。

  • 您可以在目录名、文件名或文件名后缀的任何部分使用 * 字符。

  • 如果您字面上使用 * 字符,必须用反斜杠进行转义。

pta_auth_method 指令可以指定认证方法。您可以选择查询字符串、cookie 或两者作为方法。

在两者的情况下,首先评估查询字符串,然后在查询字符串中未包含 pta 参数时评估 cookie。当查询字符串中的 pta 参数无效时,认证失败,不会回退到评估 cookie。只有在查询字符串中没有 pta 参数时,才会评估 cookie。

GitHub

您可以在 nginx-module-pta 的 GitHub 仓库 中找到此模块的其他配置提示和文档。