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

nginx+Lua环境集成、nginx+Lua应用

nginx+lua+redis实践

概述

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

1。 HttpRedis模块。

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

2。 HttpRedis2Module模块。

功能强大,比较灵活。

3。 lua-resty-redis库

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

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

OpenResty安装

安装wget

[root@localhost yum.repos.d]# yum install wget

下载资源库

[root@localhost yum.repos.d]# wget https://openresty.org/package/centos/openresty.repo

得到文件: openresty.repo 安装OpenResty

[root@localhost yum.repos.d]# yum install openresty

启动OpenResty

[root@localhost yum.repos.d]# /usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/nginx/

[root@localhost yum.repos.d]#

测试:

我们用nat,注意端口映射。

初试测试lua

修改conf

server {

listen 8080; location / {

default_type text/html;

content_by_lua 'ngx.say("hello my openresty")'; }

}

redis安装

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

[root@localhost /]# yum install epel-release

安装redis

[root@localhost /]# yum install redis

启动redis

[root@localhost /]# /usr/bin/redis-cli

Could not connect to Redis at 127.0.0.1:6379: Connection refused

Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected>

[root@localhost /]# systemctl start redis

测试redis

redis服务端和redis客户端 启动redis客户端

[root@localhost /]# cd /usr/bin/     [root@localhost /]# which redis-cli /usr/bin/redis-cli

[root@localhost bin]# ./redis-cli

127.0.0.1:6379> set akey avalue OK

127.0.0.1:6379> get akey "avalue"

127.0.0.1:6379> quit

[root@localhost bin]# pwd /usr/bin

[root@localhost bin]#

HttpRedis

修改配置文件

mValue[root@localhost conf]# cat 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; }

}

}

[root@localhost conf]#

以这个配置文件启动

[root@localhost conf]# /usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/nginx/ -c /usr/local/o 测试一下:

1。 redis中没有 key为m的 键值对。

[root@localhost conf]# curl http://localhost/1.html

I am 1 html

2。我们通过redis,设置key为m的value是:"mValue"。(m=mValue)

[root@localhost conf]# curl http://localhost/1.html mValue

扩展:

用于降级。

HttpRedis2Module

[root@localhost conf]# cat 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 {

set_unescape_uri $key 'n';

redis2_query get $key;

redis2_pass 127.0.0.1:6379; }

location /set {

set_unescape_uri $key 'n';

redis2_query set $key 'nValue';

redis2_pass 127.0.0.1:6379; }

}

}

[root@localhost conf]#

重启

[root@localhost conf]# /usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/nginx/ -c /usr/local/o 测试:

[root@localhost conf]# curl localhost/get $-1

[root@localhost conf]# curl localhost/set +OK

[root@localhost conf]# curl localhost/get $7

n1Value

[root@localhost conf]#

http://www.lryc.cn/news/615816.html

相关文章:

  • 分享一个基于Python和Hadoop的的电信客户特征可视化分析平台 基于Spark平台的电信客服数据存储与处理系统源码
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘mlflow’问题
  • leetcode2379:得到K个黑块的最少涂色次数(定长滑动窗口)
  • Boost.Asio io_service 与 线程 的分析
  • 字节:计算机存储单位
  • 算术运算符指南
  • 企业级WEB应用服务器TOMCAT — WEB技术详细部署
  • 使用Blender可视化多传感器坐标系转换
  • 从onnx模型到om模型的全自动化转化
  • 2025年APP开发趋势:4大方向重构行业格局
  • 【lucene】BlockDocsEnum 跟BlockImpactsDocsEnum 的区别
  • LeetCode 869.重新排序得到 2 的幂:哈希表+排序(一次初始化)
  • Java设计模式之开闭原则介绍与说明
  • 深入解析Go设计模式:命令模式实战
  • 分布微服务电商订单系统Rust编码开发[上]
  • Rust进阶-part6-宏
  • [激光原理与应用-224]:机械 - 机械设计与加工 - 常见的术语以及含义
  • 每日算法刷题Day60:8.10:leetcode 队列5道题,用时2h
  • 机器学习-增加样本、精确率与召回率
  • Modbus RTU转Profinet网关接在线循环Na离子实现PLC读取温度值
  • C# 中常用集合以及使用场景
  • 本地WSL部署接入 whisper + ollama qwen3:14b 总结字幕增加利用 Whisper 分段信息,全新 Prompt功能
  • Framework开发之Zygote进程2(基于开源的AOSP15)--init.rc在start zygote之后的事情(详细完整版逐行代码走读)
  • 《解锁 C++ 基础密码:输入输出、缺省参数,函数重载与引用的精髓》
  • 【Linux | 网络】数据链路层
  • 九、Linux Shell脚本:运算符与表达式
  • 开启单片机
  • 服务器硬件电路设计之 I2C 问答(三):I2C 总线上可以接多少个设备?如何保证数据的准确性?
  • 笔试——Day34
  • 亚麻云之全球加速器——CloudFront(CDN)服务入门