当前位置: 首页 > 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.lryc.cn/news/615721.html

相关文章:

  • Error: error:0308010C:digital envelope routines::unsupported at new Hash
  • node.js 学习笔记3 HTTP
  • #C语言——刷题攻略:牛客编程入门训练(八):分支控制(二)
  • Linux 虚拟机磁盘空间占满-全面清理方案
  • 【C++详解】红黑树规则讲解与模拟实现(内附红黑树插入操作思维导图)
  • 随机向量正交投影定理(Orthogonal Projection Theorem, OPT)_学习笔记
  • 【Python】常用内置模块
  • 诺基亚就4G/5G相关专利起诉吉利对中国汽车及蜂窝模组企业的影响
  • QT的常用控件说明
  • 【人工智能99问】LLaMA的训练过程和推理过程是怎么样的?(22/99)
  • vuhub Beelzebub靶场攻略
  • 智能体革命:网络安全人的角色重塑与突围指南
  • 医学统计(随机对照研究分类变量结局数据的统计策略2)
  • 用 ES|QL 做搜索从过滤到相关性、再到语义与混合检索
  • WPFC#超市管理系统(5)商品布局、顾客登录、商品下单
  • 机器学习实战·第三章 分类(1)
  • Deep Learning MNIST手写数字识别 Mac
  • 【Elasticsearch入门到落地】16、RestClient查询文档-快速入门
  • Lua的数组、迭代器、table、模块
  • 黑马SpringBoot+Elasticsearch作业2实战:商品搜索与竞价排名功能实现
  • sqli-labs-master/Less-51~Less-61
  • Lua语言变量、函数、运算符、循环
  • 【RocketMQ 生产者和消费者】- ConsumeMessageOrderlyService 顺序消费消息
  • 在windows安装colmap并在cmd调用
  • vue3前端项目cursor rule
  • 常用hook钩子函数
  • 海关 瑞数 失信企业 逆向 分析 后缀 rs
  • 从神经网络语言模型(NNLM)到Word2Vec:自然语言处理中的词向量学习
  • 【Html网页模板】炫酷科技风公司首页
  • Axure设计下的智慧社区数据可视化大屏:科技赋能社区管理