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

OpenResty + Lua + Redis 鉴权案例,适用于 x86 和 ARM 架构的 Docker 环境。

在这里插入图片描述

🐳 一、拉取 OpenResty 镜像
x86 架构

docker pull registry.cn-hangzhou.aliyuncs.com/qiluo-images/openresty:latest

ARM 架构

docker pull registry.cn-hangzhou.aliyuncs.com/qiluo-images/linux_arm64_openresty:latest

二、准备目录结构与文件
创建如下目录结构:

/data/lua/
├── conf.d/
│   └── script/
│       └── judge.lua
├── logs/
└── html/
⚙️ 三、准备 Lua 脚本
/data/lua/conf.d/script/judge.lua
lua
local redis = require "resty.redis"
local cjson = require "cjson"local red = redis:new()
red:set_timeout(1000)local ok, err = red:connect("172.16.11.10", 6379)  # 修改为redis相关
if not ok thenngx.log(ngx.ERR, "Failed to connect to Redis: ", err)return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
endok, err = red:auth("87vhqEne05u8")
if not ok thenngx.log(ngx.ERR, "Failed to authenticate with Redis: ", err)return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
endok, err = red:select(14)
if not ok thenngx.log(ngx.ERR, "Failed to select Redis database: ", err)return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
endlocal token = ngx.var.arg_tokenif not token or token == "" thenngx.header.content_type = "application/json"ngx.say(cjson.encode({status = "error", message = "token is required"}))ngx.exit(ngx.HTTP_UNAUTHORIZED)
elselocal exist, err = red:get("tk:" .. token)if not exist or exist == ngx.null thenngx.log(ngx.WARN, "Token is invalid or does not exist: ", token)ngx.header.content_type = "application/json"ngx.say(cjson.encode({status = "error", message = "token is invalid or expired"}))ngx.exit(ngx.HTTP_UNAUTHORIZED)elsengx.log(ngx.INFO, "Token is valid: ", token)end
endred:close()

🔧 四、准备 Nginx 配置文件

/data/lua/conf.d/default.confhttp {lua_package_path "/usr/local/openresty/lualib/?.lua;;";server {listen 1004;server_name localhost;root /usr/local/openresty/nginx/html/zszhjg/zsd/;location /ctis-zszhjg/ {set $token "";if ($arg_token) {set $token "tk:$arg_token";}access_by_lua_file /etc/nginx/conf.d/script/judge.lua;proxy_pass http://172.16.14.11:7000/ctis-zszhjg/;proxy_set_header Host $host;proxy_set_header x-Real-IP $remote_addr;proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for;if ($arg_filename ~ "\(.+)" ) {add_header Content-Disposition "attachment;filename=$arg_filename";}}}
}

🐘 五、准备 Redis Lua 库

cd /data/lua
git clone https://github.com/openresty/lua-resty-redis.git
cp lua-resty-redis/lib/resty/redis.lua /data/lua/conf.d/resty/redis.lua
🚀 六、启动 OpenResty 容器
bash
docker run -d \--name openresty \--restart=always \-v /etc/localtime:/etc/localtime:ro \-v /data/lua/conf.d:/etc/nginx/conf.d \-v /data/lua/logs:/usr/local/openresty/nginx/logs \-v /data/lua/html:/usr/local/openresty/nginx/html \-v /data/lua/conf.d/resty/redis.lua:/usr/local/openresty/lualib/resty/redis.lua \registry.cn-hangzhou.aliyuncs.com/qiluo-images/openresty:latest

ARM 架构 OpenResty + Lua-Redis 案例

🐳 一、拉取 OpenResty 镜像
ARM 架构

docker pull registry.cn-hangzhou.aliyuncs.com/qiluo-images/linux_arm64_openresty:latest

📁 二、准备目录结构与文件
创建目录结构:

mkdir -p /data/lua/conf.d/script
mkdir -p /data/lua/logs
mkdir -p /data/lua/html
mkdir -p /data/lua/lualib/resty

📥 三、下载 Lua-Redis 库

cd /data/lua
git clone https://github.com/openresty/lua-resty-redis.git
cp lua-resty-redis/lib/resty/redis.lua /data1/lua/lualib/resty/redis.lua

⚙️ 四、准备 Lua 脚本
/data/lua/conf.d/script/judge.lua

local redis = require "resty.redis"
local cjson = require "cjson"-- 创建 Redis 连接对象
local red = redis:new()-- 设置 Redis 连接信息
red:set_timeout(1000) -- 1秒超时
local ok, err = red:connect("172.16.11.10", 6379)
if not ok thenngx.log(ngx.ERR, "Failed to connect to Redis: ", err)return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end-- 设置 Redis 密码
ok, err = red:auth("87vhqEne05u8")
if not ok thenngx.log(ngx.ERR, "Failed to authenticate with Redis: ", err)return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end-- 选择 Redis 数据库索引
ok, err = red:select(14)
if not ok thenngx.log(ngx.ERR, "Failed to select Redis database: ", err)return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end-- 从 Nginx 变量中获取 token
local token = ngx.var.arg_token-- 检查 token 是否存在
if not token or token == "" then-- 如果没有 token 参数,返回 401 unauthorizedngx.header.content_type = "application/json"ngx.say(cjson.encode({status = "error", message = "token is required"}))ngx.exit(ngx.HTTP_UNAUTHORIZED)
else-- 检查 token 是否有有效且未过期local exist, err = red:get("tk:" .. token)if not exist or exist == ngx.null thenngx.log(ngx.WARN, "Token is invalid or does not exist: ", token)ngx.header.content_type = "application/json"ngx.say(cjson.encode({status = "error", message = "token is invalid or expired"}))ngx.exit(ngx.HTTP_UNAUTHORIZED)else-- Token 是有效的,允许请求通过ngx.log(ngx.INFO, "Token is valid: ", token)-- 这里不需要返回内容,继续执行后续的 proxy_passend
end-- 关闭 Redis 连接
red:set_keepalive(10000, 100)  -- 使用连接池,提高性能

🔧 五、准备 Nginx 配置文件
/data1/lua/conf.d/default.conf

worker_processes  auto;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;# Lua 模块路径配置lua_package_path "/usr/local/openresty/lualib/?.lua;;";lua_package_cpath "/usr/local/openresty/lualib/?.so;;";sendfile        on;keepalive_timeout  65;server {listen 1004;server_name localhost;# 静态文件根目录root /usr/local/openresty/nginx/html;location /ctis-zszhjg/ {# 设置 token 变量set $token "";if ($arg_token) {set $token "tk:$arg_token";}# Lua 鉴权脚本access_by_lua_file /etc/nginx/conf.d/script/judge.lua;# 代理到后端服务proxy_pass http://192.168.14.89:9000/ctis-zszhjg/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 文件下载头处理if ($arg_filename ~ "\(.+)" ) {add_header Content-Disposition "attachment;filename=$arg_filename";}}# 健康检查端点location /health {access_log off;return 200 "healthy\n";add_header Content-Type text/plain;}}
}

🚀 六、启动 OpenResty 容器(ARM)

docker run -d \--name openresty-arm \--restart=always \-v /etc/localtime:/etc/localtime:ro \-v /data/lua/conf.d:/etc/nginx/conf.d \-v /data/lua/logs:/usr/local/openresty/nginx/logs \-v /data/lua/html:/usr/local/openresty/nginx/html \-v /data/lua/lualib:/usr/local/openresty/lualib \registry.cn-hangzhou.aliyuncs.com/qiluo-images/linux_arm64_openresty:latest

OpenResty Manager:可视化+高性能+安全

配置文件可参考

https://github.com/Safe3/openresty-manager/blob/main/docker/docker-compose.yml

创建目录

mkdir -p /data/om_acme /data/om_data /data/om_conf /data/om_logs

拉取x86镜像

docker pull registry.cn-hangzhou.aliyuncs.com/qiluo-images/openresty-manager:latest

拉取arm64镜像

docker pull registry.cn-hangzhou.aliyuncs.com/qiluo-images/linux_arm64_openresty-manager:latest

运行命令:

docker run -d --name openresty-manager --restart always --network host -v /etc/localtime:/etc/localtime:ro -v /etc/resolv.conf:/etc/resolv.conf:ro -v /var/run/docker.sock:/var/run/docker.sock -v /data/om_acme:/opt/om/acme -v /data/om_data:/opt/om/data -v /data/om_conf:/opt/om/nginx/conf -v /data/om_logs:/opt/om/nginx/logs registry.cn-hangzhou.aliyuncs.com/qiluo-images/openresty-manager:latest

或者

docker run -d --name openresty-manager --restart always -p 80:80 -p 443:443 -p 9080:9080 -v /etc/localtime:/etc/localtime:ro -v /etc/resolv.conf:/etc/resolv.conf:ro -v /var/run/docker.sock:/var/run/docker.sock -v /data/om_acme:/opt/om/acme -v /data/om_data:/opt/om/data -v /data/om_conf:/opt/om/nginx/conf -v /data/om_logs:/opt/om/nginx/logs uusec/openresty-manager:latest

1.登录管理:访问 https://ip:9080 ,默认用户名为“admin”,默认密码为“#Passw0rd”。(登录之后不要忘记第一时间改密)

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

相关文章:

  • Redis批量查询的 4 种方式
  • Google Chrome浏览器安装教程 谷歌浏览器离线安装(PC+安卓)附谷歌三件套 安装包!
  • Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
  • 好文与笔记分享 Paris, A Decentralized Trained Open-Weight Diffusion Model
  • 企业网站托管排版设计专业网络营销外包公司
  • 1.5 欧拉集群安装Memcached缓存服务
  • asp.net 开发的网站wordpress付费下载模板
  • 十三、OpenCV中的图像的向上采样和向下采样
  • 一份面向研究人员的强化学习对齐指南:为自定义语言模型实施与评估 PPO 和 DPO
  • 石家庄网站seo网页设计与制作课程定位
  • Python全栈(基础篇)——Day10:后端内容(map+reduce+filter+sorted+实战演示+每日一题)
  • Datawhale OpenAI官方智能体框架202510
  • 25软件测试工作量估算
  • 网站页脚版权信息在线html编辑
  • 计算机视觉:卷积神经网络(CNN)图像分类从像素与色彩通道基础到特征提取、池化及预测
  • C# 串口通信完整教程 (.NET Framework 4.0)
  • GNN是和RNN一样的吗?多次循环,但是更新的是同一批参数?
  • Ubuntu 24.04 安装 Jenkins
  • 手游做网站推广应该怎么做photoshop做网站
  • 成都专业的整站优化公司起名字免费软件
  • 【threejs】webgl使用effectComposer时的抗锯齿
  • 大语言模型(LLM)领域细分方向解析
  • 简要说明开发网站的步骤谷歌搜索引擎363
  • Spotify(正版流媒体音乐平台) 多语便携版
  • 告别复制粘贴!自动化处理文本空行的新思路
  • 基于「多模态大模型 + BGE向量检索增强RAG」的新能源汽车故障诊断智能问答系统(vue+flask+AI算法)
  • 实战|京东 jd.union.open.goods.search 接口:精准检索与 2025 商业机会挖掘
  • 从零上手 Rokid JSAR:打造专属 AR 桌面交互式 3D魔方,开启空间开发之旅
  • 番禺人才网招聘信恿南昌seo数据监控
  • 自动驾驶强化学习的价值对齐:奖励函数设计的艺术与科学