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

nginx+lua+redis案例

nginx+lua+redis限流实战

先跑通基本环境,再实现具体业务。

基本环境准备

​
nginx配置文件[root@localhost conf]# vi nginx-ip-limit.confworker_processes 1;error_log logs/error.log debug;events {worker_connections 1024; }http {include mime.types;default_type application/octet-stream;server {listen 8083;location / {default_type text/html;access_by_lua_file /usr/local/openresty/nginx/lua/ip-limit-access.lua;log_by_lua_file /usr/local/openresty/nginx/lua/ip-limit-log.lua;proxy_pass http://localhost:8080/;}}}两个lua文件[root@localhost lua]# cat ip-limit-access.lua ngx.log(ngx.INFO,"ip limit access");[root@localhost lua]# cat ip-limit-log.lua ngx.log(ngx.INFO,"ip limit log");[root@localhost lua]#​

限流业务

需求:系统每秒限流2个请求,如果超过 阈值(每秒2个请求),则系统限制10秒内,不能被访问。

lua业务代码[root@localhost lua]# cat ip-limit-access.lua ngx.log(ngx.INFO,"ip limit access");local redis = require "resty.redis";local red = redis:new();--链接redisred:connect("127.0.0.1",6379);-- 需要写链接成功的判断。--判断是否限流limit = red:get("limit"); if limit == '1' thenreturn ngx.exit(503); endinc = red:incr("testLimit"); if inc <= 2 thenred:expire("testLimit",1); elsered:set("limit",1);red:expire("limit",10); end[root@localhost lua]#

防爬虫案例

概述

当爬虫影响到我们网站的性能。

爬虫的种类:

1。善意的。百度,google。

2。恶意的。恶意窃取网站内容。 robots协议:

防爬虫的方法:现在爬虫ip,对我们系统的请求。

 扩展:限制爬虫的方法:

1。限制user-agent。

2。限制ip。

3。添加验证码。

4。限制cookie

需求&步骤分解

1。收集黑名单IP。

2。存储到redis的set集合中。

3。 nginx定期(2s)去从redis取 黑名单 ip 集合。

4。当请求来的时候,进行判断。请求来源的ip是否在ip黑名单中。

Redis黑名单准备

用set类型

key:ip-black-list[root@localhost ~]# /usr/bin/redis-cli127.0.0.1:6379> sadd ip-black-list 10.0.1.1 (integer) 1127.0.0.1:6379> sadd ip-black-list 10.0.2.2 (integer) 1127.0.0.1:6379> smember ip-balck-list(error) ERR unknown command 'smember'127.0.0.1:6379> smembers ip-balck-list (empty list or set)127.0.0.1:6379> smembers ip-black-list1) "10.0.2.2"2) "10.0.1.1"127.0.0.1:6379>

编写nginx配置文件

​
[root@localhost nginx]# cat conf/nginx-black-list.conf worker_processes 1;error_log logs/error.log debug;events {worker_connections 1024; }http {## 定义共享空间lua_shared_dict ip_black_list 1m;include mime.types;default_type application/octet-stream;server {listen 8083;location / {default_type text/html;access_by_lua_file /usr/local/openresty/nginx/lua/black-list-access.lua; proxy_pass http://localhost:8080/;}}}[root@localhost nginx]#共享变量:ip_black_list​

编写核心lua脚本

http://www.dtcms.com/a/324856.html

相关文章:

  • 《Webpack与Vite热模块替换机制深度剖析与策略抉择》
  • 消息生态系统全景解析:技术架构、核心组件与应用场景
  • 【Python练习】085. 编写一个函数,实现简单的DNS服务器功能
  • LeeCode 46. 全排列
  • 【树\思维】P1395 会议
  • 33.搜索旋转排序数组
  • Agno智能体框架简单使用
  • docker等基础工具使用
  • 从策略梯度到 PPO
  • java中的继承
  • Flutter开发 LinearProgressIndicato、CircularProgressIndicator
  • django基于Python的设计师作品平台的数据可视化系统设计与实现
  • QT的常用控件说明
  • Java基础—解析注解
  • 游戏常用运行库合集:一键解决游戏兼容性问题
  • 锂电池SOH预测 | 第35讲 Matlab基于BiLSTM的锂电池健康状态估计(锂电池SOH预测),附锂电池最新文章汇集
  • scanpy单细胞转录组python教程(二):单样本数据分析之数据质控
  • springboot的基础要点
  • 【Task3】【Datawhale AI夏令营】多模态RAG
  • 3.4路由守卫
  • Words or Vision Do Vision-Language Models Have Blind Faith in Text
  • Java中new的相关知识
  • nginx-主配置文件
  • Redis的批处理优化
  • 【高等数学】第八章 向量代数与空间解析几何——第六节 空间曲线及其方程
  • ECP HRFORM 提示ADS服务异常
  • 【嵌入式电机控制#补充3】SDK电机控制台的功能
  • C9800在NAT设备之后怎么办?
  • [创业之路-541]:经营分析会 - 企业的经营分析会,研发负责人负责提供哪些信息?
  • Linux810 shell 条件判断 文件工具 ifelse