要实现这个功能,可以使用 NGINX 的 if 指令 和 $cookie 变量。具体实现步骤如下:

  1. 在 server 块中添加如下配置:
if ($cookie_a = "") {
    return 403;
}

这里使用了 if 指令 和 $cookie_a 变量,如果 cookie_a 不存在或为空,则返回 403 错误。

  1. 在 location 块中添加需要限制访问的网站配置,例如:
location / {
    root /usr/share/nginx/html;
    index index.html;
}

这里指定了网站的根目录和默认首页。

  1. 重启 NGINX 服务,使配置生效。
sudo service nginx restart

这样,当 cookie_a 不存在或为空时,访问该网站会返回 403 错误,否则可以正常访问。

使用 NGINX 限制访问:当 Cookie 'a' 不存在时拒绝访问

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

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