[BETA!] rdns: NGINX HTTP rDNS 模块
安装
您可以在任何基于 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-rdns
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-rdns
通过在 /etc/nginx/nginx.conf 顶部添加以下内容来启用该模块:
load_module modules/ngx_http_rdns_module.so;
本文档描述了 nginx-module-rdns v0,于 2020 年 6 月 8 日发布。
生产稳定性不保证。
概述
此模块允许对传入连接进行反向 DNS (rDNS) 查询,并通过允许/拒绝规则提供对传入主机名的简单访问控制(类似于 HttpAccessModule 的允许/拒绝指令;支持正则表达式)。该模块与标准解析器指令定义的 DNS 服务器一起工作。此模块在解析 DNS 查询时使用 nginx 核心解析器缓存,最大缓存时间为 30 秒或 DNS 响应 TTL。
示例
location / {
resolver 127.0.0.1;
rdns_deny badone\.example\.com;
if ($http_user_agent ~* FooAgent) {
rdns on;
}
if ($rdns_hostname ~* (foo\.example\.com)) {
set $myvar foo;
}
#...
}
在上述示例中,nginx 将对每个具有 "FooAgent" 用户代理的请求进行反向 DNS 请求(通过 127.0.0.1 DNS 服务器)。来自 badone.example.com 的请求将被禁止。$rdns_hostname 变量将包含 rDNS 请求的结果或 "not found"(如果未找到或发生任何错误)对于 FooAgent 发出的任何请求。对于其他用户代理,$rdns_hostname 将具有特殊值 "-"。
指令
rdns
- 语法: rdns on | off | double
- 默认值: -
- 上下文: http, server, location, if-in-server, if-in-location
- 阶段: rewrite
- 变量: rdns_hostname
启用/禁用 rDNS 查询。
- on - 在此上下文中启用 rDNS 查询。
- double - 在此上下文中启用双重 DNS 查询。如果反向查询(rDNS 请求)成功,模块将对其结果执行正向查询(DNS 请求)。如果此正向查询失败或没有任何正向查询 IP 地址与原始地址匹配,$rdns_hostname 将被设置为 "not found"。
- off - 在此上下文中禁用 rDNS 查询。
$rdns_hostname 变量可能具有:
- 查询结果;
- 如果未找到或请求期间发生错误,则为特殊值 "not found";
- 如果禁用查询,则为特殊值 "-"。
执行查询后,模块会重新启动请求处理管道,以使新的 $rdns_hostname 变量值对其他指令可见。
关于服务器/位置 "if" 的注意事项:
在服务器或位置的 "if" 中,模块通过重写模块代码工作。当任何启用指令(rdns on|double)首次执行时,它会启用 DNS 查询并中断(以防止在此 "if" 中执行进一步的指令)。查询完成后,使用重写模块代码的 "if" 中的指令将第二次执行,而不进行任何中断。禁用指令(rdns off)不会导致中断。
必须定义核心模块解析器以使用此指令。
rdns_allow
- 语法: rdns_allow regex
- 默认值: -
- 上下文: http, server, location
- 阶段: access
- 变量: -
授予与正则表达式匹配的域的访问权限。
rdns_deny
- 语法: rdns_deny regex
- 默认值: -
- 上下文: http, server, location
- 阶段: access
- 变量: -
禁止与正则表达式匹配的域的访问。
关于访问列表的注意事项
rdns_allow 和 rdns_deny 指令为其使用的上下文定义了一个新的访问列表。
上下文中的访问列表继承仅在子上下文未定义自己的规则时有效。
关于命名位置的警告
在命名位置中进行 rDNS 请求不受支持,可能会引发循环。例如:
server {
rdns on;
location / {
echo_exec @foo;
}
location @foo {
#...
}
}
在命名位置中并重新启动请求处理管道时,nginx 将继续在通常的(未命名)位置中处理请求。这就是为什么如果您不在命名位置中禁用模块,此示例将导致循环。此示例的正确配置应如下所示:
server {
rdns on;
location / {
echo_exec @foo;
}
location @foo {
rdns off;
#...
}
}
链接
- GitHub 上的源代码: https://github.com/flant/nginx-http-rdns
- 模块主页(俄语): http://flant.ru/projects/nginx-http-rdns
GitHub
您可以在 nginx-module-rdns 的 GitHub 仓库 中找到此模块的其他配置提示和文档。