当前位置: 首页 > news >正文

Nginx + Lua 实现每日访问次数限制与防盗链校验

🧾 Nginx + Lua 实现每日访问次数限制与防盗链校验(以 /cmap 图片接口为例)

一、应用场景

/cmap 是一个图片接口(通过 proxy_pass 转发到后端),
需要实现:

  1. 每日最多访问 1000 次
  2. 防盗链检查(仅允许特定来源 Referer)
  3. 当返回 403 / 429 时输出 JSON 格式提示
  4. 正常访问时返回图片内容(不影响 Content-Type)

二、依赖模块

要启用 access_by_lua_block,Nginx 需要编译 ngx_http_lua_module 模块。
如果是自己构建镜像,请确保编译参数中包含:

--add-module=/path/to/lua-nginx-module

并在 Nginx 配置文件中引入 Lua 共享内存:

lua_shared_dict access_limit 10m;

三、核心配置示例

location /cmap {proxy_pass http://10.18.55.96/cmap/layer;access_by_lua_block {-- 每日访问上限local limit = 1000local key = "cmap_access_count"local dict = ngx.shared.access_limitlocal day = os.date("%Y%m%d")local count_key = key .. ":" .. day-- ======================-- 防盗链检查-- ======================local headers = ngx.req.get_headers()local referer = headers["referer"]-- 允许的来源列表(使用正则匹配)local allowed_domains = {"^https?://10%.18%.55%.98:30001/","^https?://yourdomain%.com/"}-- 是否允许空 Referer(例如浏览器直接访问)local allow_empty_referer = truelocal valid = falseif referer thenfor _, pattern in ipairs(allowed_domains) doif referer:match(pattern) thenvalid = truebreakendendelseif allow_empty_referer thenvalid = trueendif not valid thenngx.status = 403ngx.header.content_type = "application/json; charset=utf-8"ngx.say('{"code":403,"message":"Forbidden: Invalid Referer"}')return ngx.exit(403)end-- ======================-- 每日访问次数统计-- ======================local current = dict:get(count_key)if not current thendict:set(count_key, 0, 86400)  -- 有效期1天current = 0endif current >= limit thenngx.status = 429ngx.header.content_type = "application/json; charset=utf-8"ngx.say(string.format('{"code":429,"message":"Daily access limit exceeded","count":%d,"limit":%d}',current, limit))return ngx.exit(429)endlocal new_count = dict:incr(count_key, 1)if not new_count thendict:set(count_key, current + 1, 86400)new_count = current + 1endngx.log(ngx.INFO, string.format("cmap accessed %d/%d times today", new_count, limit))}
}

四、逻辑说明

功能 说明
防盗链 校验 Referer 是否来自白名单域名或允许为空
访问计数 使用 ngx.shared.DICT 记录每日访问次数
时间粒度 以当天日期为 key(如 20251031
响应格式 正常请求 → 返回图片;403 / 429 → 返回 JSON
过期机制 每天自动重置计数(缓存过期 86400 秒)

五、Referer 匹配规则

写法 匹配示例 说明
"http://10.18.55.98:30001/" 仅匹配该路径开头(string.find 简单匹配
"^https?://10%.18%.55%.98:30001/" 匹配 http 或 https 前缀 推荐正则匹配
"^https?://yourdomain%.com/" 匹配自定义域名 正则匹配更安全

六、其他建议

  • 若服务器被直接访问(无 Referer),可启用 allow_empty_referer = true
  • 若需更精细限制(如按 IP 统计、按小时限流),可在 key 中加上 ngx.var.remote_addros.date("%H")
  • 若使用 Docker 运行,需要在镜像中包含 lua-nginx-moduleluajit
http://www.gsyq.cn/news/35982.html

相关文章:

  • 2025年北京保洁服务公司推荐:海獭顾家保洁服务公司深度测评
  • 三角函数:从入门到入门
  • 学术会议合集 | 机器人、大数据、社会科学、医学人工智能等EI会议合集
  • 基于块匹配的MATLAB视频去抖动算法
  • 2025年不锈钢带制造厂推荐:五大不锈钢带制造企业深度解析
  • 2025年ITSM平台演进趋势与选型指南:大模型智能体引领、数据AI底座支撑、业务价值驱动运维决策
  • 电视剧推荐《脱轨》
  • 于鸿硕面向对象设计大作业02
  • 2025年10月小学生学习机品牌评测:五强榜单性能与口碑全解析
  • 2025 年 PCB 打板做板,PCBHDI 高密度互连板,PCB 电路板线路板厂家最新推荐,技术实力与市场口碑深度解析
  • 2025年10月小学生学习机品牌榜单:销量数据与功能对比全解析
  • 【ACM出版 | ACM出版社目前快至见刊后1个月EI、Scopus检索】2025年数字化社会与智能计算国际学术会议 (ICDSIC 2025)
  • (论文阅读)ENMA: Tokenwise Autoregression for Generative Neural PDE Operators
  • 2025济南单招综评培训/班/机构推荐榜:济南易升教育五星领跑!山东本地化定制+高通过率,3企凭特色突围​
  • 2025 年尼丝纺里布,胆布里布,高弹里布,四面弹里布厂家最新推荐,技术实力与市场口碑深度解析
  • 2025年10月性价比高的挖掘机品牌推荐:口碑排名榜
  • 2025年10月小型挖掘机售后保障排行:五品牌实测对比
  • HttpRunner-8-数据驱动-httprunner-8
  • HttpRunner-7-用例分层-httprunner-7
  • HttpRunner-5-DebugTalk-httprunner-5
  • 制造业运营管理新突破:Tita 赋能下的精益生产与协同发展
  • Dify入门实战:5分钟搭建你的第一个AI测试用例生成器
  • .NET Core + Docker 部署:Serilog 日志配置与查看全攻略
  • 2025年10月国内环保工程设备厂家前5名推荐榜单
  • 2025 年加药装置厂家最新推荐榜,技术实力与市场口碑深度解析,精选高适配性优质品牌加药装置/江苏加药装置公司推荐
  • 2025年10月教育资源好的学习机品牌推荐:销量榜评测报告
  • 《星球大战:克隆人战争》中的片头格言
  • matlab byte 转 bit 函数编写
  • 深入解析:嵌入式硬件——基于IMX6ULL的GPT(通用定时器)实现
  • 2025年10月教育资源好的学习机品牌推荐:实力榜全面评测