Nginx 通过机器名而不是 IP 地址限制访问

Nginx restricting access by machine name instead of IP address

我将 nginx 与简单的身份验证规则一起使用,例如:

location /api {
    #...
    deny  192.168.1.2;
    allow 10.128.99.1;
    allow 127.0.0.1;
    deny  all;
}

这很好用,但有时用户 IP 会更改,然后我必须使用新 IP 更新 nginx.conf 文件。有没有办法用机器名代替,比如下面的:

location /api {
    #...
    deny  192.168.1.2;
    allow MachineName;
    allow 127.0.0.1;
    deny  all;
}

我试过了,但是当我尝试启动服务时,nginx 报错了。我可以使用 ping 来查找 IP,但我只是想自动执行该过程。提前致谢。

Nginx HTTP rDNS module 可以满足您的要求:

This module allows to make a reverse DNS (rDNS) lookup for incoming connection and provides simple access control of incoming hostname by allow/deny rules

唯一的缺点是您将 have to disable HTTP/1.1 keepalive 由于未修复的突出错误。

可以使用 commercial GetPageSpeed repository 在 CentOS 系统(6、7 和 8)上安装(需要订阅):

sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
sudo yum -y install nginx-module-rdns