使用 Nginx 的 map 指令结合时间变量来实现每天不同时间段返回 403

Posted by he; tagged with nginx

完整测试配置示例

http {

#$time_iso8601变量设置时间段,此处为8点至18点访问返回403
map $time_iso8601 $deny_access {
    default 0;
    # 匹配ISO8601格式时间中的小时部分
    "~T(08|09|10|11|12|13|14|15|16|17|18):" 1;
}

    server {
        listen 80;        
        location / {

            if ($deny_access) {
                return 403 "Access Denied between 03:00-19:00";
            }
            
            # 正常访问内容

        }
    }
}
- 本文完 -