可以使用 nginx 的 'ngx_http_access_module' 模块来实现这个需求。

首先,在 nginx 的配置文件中定义两个 'server' 块,分别绑定两个域名,如下所示:

server {
    listen 80;
    server_name domain1.com;
    root /var/www/domain1;

    # 其他配置
}

server {
    listen 80;
    server_name domain2.com;
    root /var/www/domain2;

    # 其他配置
}

然后,在需要限制特定 IP 访问的 'server' 块中,使用 'allow' 和 'deny' 指令来限制 IP 访问,如下所示:

server {
    listen 80;
    server_name domain1.com;
    root /var/www/domain1;

    # 只允许 192.168.1.100 的 IP 访问
    # 其他 IP 都将返回 403 Forbidden
    location / {
        deny all;
        allow 192.168.1.100;
        # 其他配置
    }
}

这样,只有 IP 地址为 192.168.1.100 的客户端才能访问 'domain1.com',其他客户端访问时将返回 403 Forbidden 错误。同时,'domain2.com' 不受任何 IP 限制,所有客户端都可以访问。

Nginx 域名访问限制:特定 IP 访问特定域名 (不使用 if 语句)

原文地址: http://www.cveoy.top/t/topic/nURu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录