nginx+lua+redis实践

nginx+lua+redis实践

1.概述

nginx、lua访问redis的三种方式:

  1. HttpRedis模块。

指令少,功能单一,适合简单的缓存。只支持get 、select命令。

  1. HttpRedis2Module模块。

功能强大,比较灵活。

  1. lua-resty-redis库

OpenResty。api。适合复杂业务,节省内存。

OpenResty:基于nginx开源版本的一个扩展版本。集成了大量的精良的lua库。

2.OpenResty安装

安装wget

 yum install wget

下载资源库

wget https://openresty.org/package/centos/openresty.repo

得到文件: openresty.repo

安装OpenResty

 yum install openresty

启动OpenResty,也可以进去到sbin目录下,执行./nginx进行启动

/usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/nginx/

OpenResty安装失败参考:https://blog.csdn.net/weixin_57147852/article/details/132800949

测试访问:

image-20231220095827356

初试测试lua,修改conf

server {listen 8080;location / {default_type text/html;content_by_lua 'ngx.say("hello my openresty")';}
}

image-20231220100723342

3.redis安装

安装epel:第三方的源(软件安装包)

yum install epel-release

安装redis

yum install redis

启动redis

systemctl start redis

测试redis

redis服务端和redis客户端

which redis-cli
# /usr/bin/redis-cli
cd /usr/bin/
./redis-cli

image-20231220101204634

4.HttpRedis

修改配置文件,在配置文件目录下cp一份 nginx.conf 得到nginx-httpredis.conf文件,修改该文件并使用该文件进行启动

nginx-httpredis.conf

worker_processes  1;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  www.cpf.com;root html;index index.html;location / {default_type text/plain;set $redis_key "m";redis_pass 127.0.0.1:6379;error_page 404 = @fetch;}location @fetch {root html;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}

在sbin目录下执行

./nginx -c /usr/local/openresty/nginx/conf/nginx-httpredis.conf

测试一下:

  1. redis中没有 key为m的 键值对,在/usr/local/openresty/nginx/html目录下创建1.html内容如下

    hello llp
    

    image-20231220105623277

  2. 我们通过redis,设置key为m的value是:“llp”(m=llp)

img

扩展:用于降级,当redis中存在某个key时,就访问缓存中的数据,不存在则正常访问页面,当流量过大时我们将redis对应的限流的key进行赋值就可以实现限流

5.HttpRedis2Module

修改配置文件,在配置文件目录下cp一份 nginx.conf 得到nginx-httpRedis2Module.conf文件,修改该文件并使用该文件进行启动

nginx-httpRedis2Module.conf

worker_processes  1;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  www.cpf.com;root html;index index.html;location /get {redis2_query get $key;redis2_pass 127.0.0.1:6379;}location /set {redis2_query set $key 'nValue';redis2_pass 127.0.0.1:6379;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}}

image-20231220124845461

6.openresty-lua-redis

参考地址:https://github.com/openresty/lua-resty-redis

修改配置文件,在配置文件目录下cp一份 nginx.conf 得到nginx-openresty-lua-redis.conf文件,修改该文件并使用该文件进行启动

nginx-openresty-lua-redis.conf

worker_processes  1;error_log  logs/error.log;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  localhost;location / {# 请求没有指定内容类型,Nginx会返回HTML内容。default_type text/html;content_by_lua_file /usr/local/openresty/nginx/lua/lua-openresty-redis.lua;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}

lua-openresty-redis.lua

-- 引用resty的redis
local redis = require "resty.redis";
local red = redis:new();-- 连接redis
local ok,err = red:connect("127.0.0.1",6379);
if not ok thenngx.say("faild to connect",err);return
endok,err = red:set("dKey","dValue");
if not ok thenngx.say("failed to set dKey",err);return
end
ngx.say("dKey set dValue success")
return

测试效果

image-20231220143958447

image-20231220144013862

分析OpenResty响应信息:

目的:为了修改以后的响应信息。

 server {listen 8081;location / {default_type text/html;content_by_lua_block {ngx.say("hi block");}}}

7.获取请求参数信息

修改配置文件,在配置文件目录下cp一份 nginx.conf 得到nginx-param.conf文件,修改该文件并使用该文件进行启动

nginx-param.conf

worker_processes  1;error_log  logs/error.log;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  localhost;location / {# 请求没有指定内容类型,Nginx会返回HTML内容。default_type text/html;content_by_lua_file /usr/local/openresty/nginx/lua/lua-http-param.lua;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}

编写lua脚本,在/usr/local/openresty/nginx/lua目录下创建lua-http-param.lua文件

lua-http-param.lua

-- 获取get请求的参数
local arg = ngx.req.get_uri_args();
for k,v in pairs(arg)
dongx.say("key:",k," value:",v);
end

将请求参数保存到redis中:

-- 获取get请求的参数
local redis = require "resty.redis";
local red = redis:new();
red:connect("127.0.0.1",6379);
-- 省去链接错误的判断,前面课程中有
local arg = ngx.req.get_uri_args();
for k,v in pairs(arg)
dongx.say("key:",k," value:",v);red:set(k,v);
end

测试: http://192.168.182.111/?id=1

8.获取请求头参数

修改配置文件,在配置文件目录下cp一份 nginx.conf 得到nginx-header.conf文件,修改该文件并使用该文件进行启动

nginx-header.conf

worker_processes  1;error_log  logs/error.log;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  localhost;location / {# 请求没有指定内容类型,Nginx会返回HTML内容。default_type text/html;content_by_lua_file /usr/local/openresty/nginx/lua/lua-http-header-param.lua;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}

编写lua脚本,在/usr/local/openresty/nginx/lua目录下创建lua-http-header-param.lua文件

lua-http-header-param.lua

-- 获取header参数
local headers = ngx.req.get_headers();
for k,v in pairs(headers)
dongx.say("[header] key:",k," value:",v);
end

测试:

image-20231220163450595

9.获取post body 键值对 参数

修改配置文件,在配置文件目录下cp一份 nginx.conf 得到nginx-body.conf文件,修改该文件并使用该文件进行启动

nginx-body.conf

worker_processes  1;error_log  logs/error.log;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  localhost;location / {# 请求没有指定内容类型,Nginx会返回HTML内容。default_type text/html;content_by_lua_file /usr/local/openresty/nginx/lua/lua-body.param.lua;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}

编写lua脚本,在/usr/local/openresty/nginx/lua目录下创建lua-body.param.lua文件

lua-body.param.lua

-- 获取post body kv参数
-- 重要:读取body
ngx.req.read_body();
local postArgs = ngx.req.get_post_args();
for k,v in pairs(postArgs)
dongx.say("[post] key:",k," value:",v);
end

测试:

10.nginx+lua+redis限流实战

限流业务

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

image-20231225102945101

修改配置文件,在配置文件目录下cp一份 nginx.conf 得到nginx-lmit-ip.conf文件,修改该文件并使用该文件进行启动

nginx-lmit-ip.conf

worker_processes  1;
error_log  logs/error.log;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       8083;server_name  localhost;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/;}}
}

限流脚本ip-limit-access.lua

ngx.log(ngx.INFO,"ip limit access");
local redis = require "resty.redis";
local red = redis:new();
--链接redis
local ok,err = red:connect("127.0.0.1",6379);
-- 需要写链接成功的判断。
if not ok thenngx.say("faild to connect",err);return
end--判断是否限流,limit为限流标记1表示需要进行限流
limit = red:get("limit");
if limit == '1' thenreturn ngx.exit(503);
end
inc = red:incr("testLimit");
if inc <= 2 thenred:expire("testLimit",1);
elsered:set("limit",1);red:expire("limit",10);
end

编写日志脚本ip-limit-log.lua

ngx.log(ngx.INFO,"ip limit log");

测试限流:

每秒两个请求以内

image-20231225103650098

每秒请求超过两次请求,则会进行限流

image-20231225103720230

11.防爬虫

概述

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

爬虫的种类:

  1. 善意的。百度,google。

  2. 恶意的。恶意窃取网站内容。

robots协议:

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

扩展:限制爬虫的方法:

  1. 限制user-agent。

  2. 限制ip。

  3. 添加验证码。

  4. 限制cookie

这里以限制ip为例:

image-20231225104400262

需求&步骤分解

  1. 收集黑名单IP

  2. 存储到redis的set集合中

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

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

Redis黑名单准备

用set类型

key:ip-black-list

192.168.25.69

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/578474.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

史上最详细的JAVA学生信息管理系统(MySQL实现)

一、项目介绍 为了巩固Java的学习写了一个基于MVC设计模式的学生管理系统。 简单介绍一下MVC设计模式&#xff1a; 1、M也就是Model 模型层&#xff08;也叫数据层&#xff09;主要是通过这个类来进行数据的操作。 2、V是Views 视图层&#xff0c;主要就是来显示页面信息。 3、…

开发辅助一(网关gateway+ThreadLocal封装用户信息+远程调用+读取配置文件+统一异常处理)

网关gateway模块 ①、配置文件&#xff0c;添加各个服务模块的路由路径 gateway:routes:-id: server-cart #微服务名称uri: lb://service-cart #负责均衡predicates:- Path/api/order/cart/**ThreadLocal ①、定义一个工具类 public class AuthContextUtil{private static…

新奇性搜索(Novelty Search,NS)

新奇性搜索&#xff08;Novelty Search&#xff0c;NS&#xff09;是一种基于进化算法的搜索优化方法&#xff0c;它通过追求新颖性来引导进化过程&#xff0c;而不是仅仅追求目标函数的优化。这种方法的灵感来自于自然进化中的创新和多样性。通过发现新的行为模式&#xff0c;…

【Redis】缓存预热

目录 常见缓存Redis客户端设计缓存key缓存预热 常见缓存 ● Redis&#xff08;分布式缓存&#xff09; ● memcached&#xff08;分布式&#xff09; ● Etcd&#xff08;云原生架构的一个分布式存储&#xff0c;存储配置&#xff0c;扩容能力&#xff09; ● ehcache&#xf…

Facebook自动回复脚本编写教程

在数字时代&#xff0c;社交媒体已经成为人们交流和建立联系的重要渠道&#xff0c;Facebook作为全球最大的社交媒体平台之一&#xff0c;拥有数十亿的用户&#xff0c;为企业和个人提供了无限的社交可能性。 然而&#xff0c;对于企业和个人来说&#xff0c;在Facebook上保持…

脱壳后多dex文件合并进apk反编译

我们遇到加固后的apk&#xff0c;在脱壳后有很多dex文件&#xff0c;有时候我们只反编译有关键代码的dex会存在一些上下文代码找不到的情况&#xff0c;这时候我们需要多dex一起反编译&#xff0c;并且需要同步看看资源文件怎么办&#xff1f;&#xff1a; 我们可以把多dex塞回…

华为eNSP入门实验,Vlan配置,路由配置,用户模式,链路聚合

文章目录 一、同一交换机下的PC通信二、不交换机下的PC通信三、配置静态路由通信四、路由器rip协议配置五、路由器ospf协议配置六、单臂路由七、通过三层交换机使不同的Vlan能连通八、设备consolo密码模式九、设备consolo用户密码模式&#xff08;AAA模式&#xff09;十、Telne…

Centos7中KeepAlivedNginx高可用

Centos7中KeepAlived&Nginx高可用 一.部署前规划 角色操作系统IPmasterCentos7.8192.168.56.101backupCentos7.8192.168.56.102 二.keepalived安装 在两台机器上执行安装 yum install -y keepalived三.master角色配置&#xff08;192.168.56.101&#xff09; /etc/kee…

虚拟机服务器中了lockbit2.0/3.0勒索病毒怎么处理,数据恢复应对步骤

网络技术的不断发展也为网络威胁带来了安全隐患&#xff0c;近期&#xff0c;对于许多大型企业来说&#xff0c;许多企业的虚拟机服务器系统遭到了lockbit2.0/3.0勒索病毒攻击&#xff0c;导致企业所有计算机系统瘫痪&#xff0c;无法正常工作&#xff0c;严重影响了企业的正常…

Python能做大项目(6)Poetry -- 项目管理的诗和远方之一

[Poetry] 是一个依赖管理和打包工具。Poetry 的作者解释开发 Poetry 的初衷时说&#xff1a; 通过前面的案例&#xff0c;我们已经提出了一些问题。但不止于此。 当您将依赖加入到 requirements.txt 时&#xff0c;没有人帮你确定它是否与既存的依赖能够和平共处&#xff0c;这…

电脑监控软件排行榜(电脑监控软件隐藏安装)

在当今数字化时代&#xff0c;电脑已经成为我们工作、学习和生活中不可或缺的工具。然而&#xff0c;随着电脑使用的普及&#xff0c;电脑监控软件也逐渐浮出水面。这类软件可以对电脑进行全方位的监控和管理&#xff0c;保护电脑安全、提高工作效率。 本文将为您介绍电脑监控…

Unity is running with Administrator privileges, which is not supported

Unity is running with Administrator privileges, which is not supported 如果还是弹出CMD窗口提示输入密码&#xff0c;但无法怎样都无法输入&#xff0c;请关闭窗口&#xff0c;然后右键快捷方式管理员运行一次。 ----------分割线---------- 为什么这样做&#xff1f; 很…

【译文】IEEE白皮书 6G 太赫兹技术的基本原理 2023版

第一章 简介 太赫兹波是介于微波和光波之间的光谱区域&#xff0c;频率从 0.1THz ~ 10THz 之间&#xff0c;波长在 3mm ~ 30μm 之间。提供大块连续的频带范围以满足对 Tbit/s 内极高数据传输速率的需求&#xff0c;使该区域成为下一代无线通信&#xff08;6G&#xff09;的重…

数字化时代的探索:学生为何对数据可视化趋之若鹜?

随着信息时代的迅猛发展&#xff0c;数据已经成为我们生活中不可或缺的一部分。而在这个数字化浪潮中&#xff0c;越来越多的学生开始关注数据可视化&#xff0c;这并非偶然。下面&#xff0c;我就从可视化从业者的角度出发&#xff0c;简单聊聊为什么越来越多的学生开始关注数…

在离线环境下也能展示地理空间数据?快来收下这份操作文档

《四维轻云-离线版》是一款操作简单、支持离线展示及编辑的地理空间数据离线管理平台。在《四维轻云-离线版》中&#xff0c;用户可以展示及编辑倾斜模型(.osgb)、激光点云(.las)、正射影像(dom)、数字高程模型(dem)、矢量数据(shp)、人工模型&#xff08;.obj、.dae、.fbx&…

Midjourney V6 引爆社交媒体,AI图像与照片的差别消失;LangChain的2023AI发展状况总结

&#x1f989; AI新闻 &#x1f680; Midjourney V6 引爆社交媒体&#xff0c;AI图像与照片的差别消失 摘要&#xff1a;Midjourney V6 第二次社区评价震惊网友&#xff0c;神图细节逼真&#xff0c;光影效果逆天&#xff0c;皮肤质感细腻&#xff0c;已超越昨日版本。V6即将…

轻松祛除烦人水印:三款简单易用的图片去水印工具介绍

在图像编辑中&#xff0c;去除水印是一项常见但又相对复杂的任务。图片水印往往成为图像处理和美化过程中的障碍。为了帮助你应对这个问题&#xff0c;下面介绍四款简单易用、效果显著的图片去水印工具。 图片去水印工具一&#xff1a;水印云 工具简介&#xff1a;水印云是一款…

181.【2023年华为OD机试真题(C卷)】查找接口成功率最优时间段(深度优先搜索(DFS)实现JavaPythonC++JS)

请到本专栏顶置查阅最新的华为OD机试宝典 点击跳转到本专栏-算法之翼:华为OD机试 🚀你的旅程将在这里启航!本专栏所有题目均包含优质解题思路,高质量解题代码,详细代码讲解,助你深入学习,深度掌握! 文章目录 【2023年华为OD机试真题(C卷)】查找接口成功率最优时…

高性能内存队列Disruptor入门和实战

目录 Disruptor简介 Disruptor的设计方案 RingBuffer数据结构 一个生产者单线程写数据的流程 多个生产者写数据的流程 消费者读数据 多个生产者写数据 Disruptor核心概念 Disruptor的使用 单生产者单消费者模式 单生产者多消费者模式 多生产者多消费者模式 消费者…

AI协助信息安全的药物发现

人工智能工具使公司能够共享候选药物数据,同时保证敏感信息的安全,可以释放机器学习和尖端实验室技术的潜力,以实现共同利益。来自:AI can help to speed up drug discovery — but only if we give it the right data, nature, 2023 目录 研究内容生物科技与AI联邦学习主动…