nginx整合lua、jwt、cjson、redis、mysql模块镜像构建
迪丽瓦拉
2025-05-29 03:12:50
0

文章目录

  • 一、基础组件下载
  • 二、组件本地安装
  • 三、镜像构造
    • 容器内部构建
    • 提交本地容器作为镜像
    • 构建最终镜像

基于centos:centos7.9.2009基础镜像,nginx整合lua、jwt、cjson、redis、mysql模块,构建基础镜像,为实现灰度/蓝绿发布提供参考,避免了频繁的构建,同时也为服务上云提供镜像支持。 ponylee/centos7-nginx:latest 镜像

一、基础组件下载

  1. luajit2-2.1-20220411.tar.gz #luajit官网存在一定的坑,下载openresty的优化版本
    https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20220411.tar.gz
  2. lua-nginx-module-0.10.22.tar.gz # 0.10.16 以后都需要 lua-resty-core和lua-resty-lrucache
    https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.22.tar.gz
  3. lua-resty-core-0.1.24.tar.gz [lua-nginx-module依赖]
    https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.24.tar.gz
  4. lua-resty-lrucache-0.12.tar.gz [lua-nginx-module依赖]
    https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.12.tar.gz
  5. ngx_devel_kit-0.3.2.tar.gz
    https://github.com/vision5/ngx_devel_kit/archive/refs/tags/v0.3.2.tar.gz
  6. nginx-1.21.6.tar.gz
    http://nginx.org/download/nginx-1.21.6.tar.gz
  7. lua-cjson-2.1.0.9.tar.gz
    https://github.com/openresty/lua-cjson/archive/refs/tags/2.1.0.9.tar.gz
  8. lua-resty-string-0.15.tar.gz
    https://github.com/openresty/lua-resty-string/archive/refs/tags/v0.15.tar.gz
  9. lua-resty-jwt-0.1.11.tar.gz
    https://github.com/SkyLothar/lua-resty-jwt/archive/refs/tags/v0.1.11.tar.gz
  10. lua-resty-hmac-0.06.tar.gz
    https://github.com/jkeys089/lua-resty-hmac/archive/refs/tags/v0.06.tar.gz
  11. lua-resty-redis-0.29.tar.gz
    https://github.com/openresty/lua-resty-redis/archive/refs/tags/v0.29.tar.gz
  12. lua-resty-mysql-0.26.tar.gz
    https://github.com/openresty/lua-resty-mysql/archive/refs/tags/v0.26.tar.gz

二、组件本地安装

  1. luajit安装

编译

cd /opt/lua/
tar -zxvf luajit2-2.1-20220411.tar.gz
cd luajit2-2.1-20220411
make install PREFIX=/usr/local/luajit
--卸载
make uninstall PREFIX=/usr/local/luajit

加载

ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/liblua-5.1.so.2
echo "/usr/local/luajit/lib" >> /etc/ld.so.conf
ldconfig

配置环境变量

vi /etc/profile
#增加下面内容
echo "export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1" >> /etc/profile
echo "export LUAJIT_LIB=/usr/local/luajit/lib" >>/etc/profile
#立刻生效
source /etc/profile
  1. lua-resty-core安装
cd /opt/lua/
tar -zxvf lua-resty-core-0.1.24.tar.gz
cd lua-resty-core-0.1.24
make install PREFIX=/usr/local/lua_core
  1. lua-resty-lrucache安装
cd /opt/lua/
tar -zxvf lua-resty-lrucache-0.12.tar.gz
cd lua-resty-lrucache-0.12
make install PREFIX=/usr/local/lua_core
  1. lua-nginx-module安装
cd /opt/lua/
tar -zxvf lua-nginx-module-0.10.22.tar.gz
  1. ngx_devel_kit安装
cd /opt/lua/
tar -zxvf ngx_devel_kit-0.3.2.tar.gz
  1. nginx加载lua模块
cd /opt/lua/
tar -zxvf nginx-1.21.6.tar.gz
cd nginx-1.21.6/ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx  --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx \
--with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' \
--add-module=/opt/lua/ngx_devel_kit-0.3.2 \
--add-module=/opt/lua/lua-nginx-module-0.10.22make -j2 && make install

需要特殊配置:

--prefix=/etc/nginx 
--sbin-path=/usr/sbin/nginx
--add-module=/opt/lua/ngx_devel_kit-0.3.2 
--add-module=/opt/lua/lua-nginx-module-0.10.22

增加lua测试代码

location /hello { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; 
}

nginx.conf配置文件中,http模块增加包依赖

lua_package_path "/usr/local/lua_core/lib/lua/?.lua;;";

检查配置
/opt/nginx/sbin/nginx -t
加载配置
/opt/nginx/sbin/nginx -s reload

访问curl http://127.0.0.1/hello,返回hello,lua,说明nginx集成lua成功

至此已经可以使用lua基本功能了,笔者因想使用lua实现jwt校验,所以后续还需要安装cjson和retry-http

  1. lua-cjson安装
cd /opt/lua
tar -zxvf lua-cjson-2.1.0.10.tar.gz
cd lua-cjson-2.1.0.10

修改Makefile,指定luajit

vi Makefile
将LUA_INCLUDE_DIR的值$(PREFIX)/include改为/usr/local/luajit/include/luajit-2.1make && make install PREFIX=/usr/local/cjson
#cp /usr/local/cjson/lib/lua/5.1/cjson.so /usr/local/luajit/lib/lua/5.1/
cp /usr/local/cjson/lib/lua/5.1/cjson.so /usr/local/lib/lua/5.1
chmod 755 /usr/local/lib/lua/5.1/cjson.so

验证
cd /usr/local/luajit/bin
./luajit
print(require “cjson”)
–或者
local json = require(“cjson”)

  1. lua-resty-string安装
cd /opt/lua/
tar -zxvf lua-resty-string-0.15.tar.gz
cd lua-resty-string-0.15
make install PREFIX=/usr/local/lua_core

9.lua-resty-jwt安装

cd /opt/lua/
tar -zxvf lua-resty-jwt-0.1.11.tar.gz
cp lua-resty-jwt-0.1.11/lib/resty/* /usr/local/lua_core/lib/lua/resty
  1. lua-resty-hmac安装
cd /opt/lua/
tar -zxvf lua-resty-hmac-0.06.tar.gz
cp lua-resty-hmac-0.06/lib/resty/* /usr/local/lua_core/lib/lua/resty
  1. lua-resty-redis安装
cd /opt/lua/
tar -zxvf  lua-resty-redis-0.29.tar.gz
cp  lua-resty-redis-0.29/lib/resty/* /usr/local/lua_core/lib/lua/resty
  1. lua-resty-mysql安装
cd /opt/lua/
tar -zxvf  lua-resty-mysql-0.26.tar.gz
cp  lua-resty-mysql-0.26/lib/resty/* /usr/local/lua_core/lib/lua/resty

至此,redis、mysql、lua和jwt都集成完成,可以通过下面样例进行测试

nginx.conf配置文件中,http模块增加包依赖
lua_package_path “/usr/local/lua_core/lib/lua/?.lua;;”;

增加lua测试代码

location /verify {default_type text/html;content_by_lua 'local redis = require("resty.redis")local mysql = require("resty.mysql")local cjson = require "cjson"local jwt = require "resty.jwt"local secret = "lua-resty-jwt"local jwt_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" ..".eyJmb28iOiJiYXIifQ" ..".VAoRL1IU0nOguxURF2ZcKR0SGKE1gCbqwyh8u2MLAyY"local jwt_obj = jwt:verify(secret, jwt_token)ngx.say(jwt_obj.verified)ngx.say(cjson.encode(jwt_obj))';
}

访问:

curl http://127.0.0.1/verify

返回:

{"reason": "everything is awesome~ :p","valid": true,"payload": {"foo": "bar"},"signature": "VAoRL1IU0nOguxURF2ZcKR0SGKE1gCbqwyh8u2MLAyY","raw_payload": "eyJmb28iOiJiYXIifQ","header": {"alg": "HS256","typ": "JWT"},"raw_header": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9","verified": true}

三、镜像构造

容器内部构建

启动容器

docker run -itd --name centos -p 8080:80 \
-v /opt/lua:/opt/lua \
centos:centos7.9.2009

复制编译完成的文件

docker  cp   /etc/nginx centos:/etc
docker  cp   /usr/sbin/nginx centos:/usr/sbin
docker cp    /usr/local/luajit centos:/usr/local
docker cp    /usr/local/lua_core centos:/usr/local
docker cp    /usr/local/cjson/lib/lua/5.1/cjson.so  centos:/usr/local/lib/lua/5.1

容器内部执行

#加载ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/liblua-5.1.so.2echo "/usr/local/luajit/lib" >> /etc/ld.so.confldconfig#配置环境变量echo "export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1" >> /etc/profileecho "export LUAJIT_LIB=/usr/local/luajit/lib" >>/etc/profilesource /etc/profile

提交本地容器作为镜像

docker commit -m=“centos7.9.2009 with nginx/1.21.6” -a=“pony” centos ponylee/centos7-nginx:v1

构建最终镜像

Dockerfile

FROM ponylee/centos7-nginx:v1
MAINTAINER ponylee
# 注意,日志软链,控制台打印日志
RUN  ln -sf /dev/stdout /var/log/nginx/access.log && \ln -sf /dev/stderr /var/log/nginx/error.logEXPOSE 80
EXPOSE 443
VOLUME [ "/var/log/nginx","/etc/nginx/conf.d" ]CMD ["nginx", "-g", "daemon off;"]

构建

docker build -t ponylee/centos7-nginx:latest ./

启动容器

docker run --rm --name=ngx_lua -it  -p 8088:80 \
-v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:rw \
-v /etc/nginx/conf.d:/etc/nginx/conf.d:rw \
-v /data/nginx:/var/log/nginx:rw \ # 日志挂载,控制台不打印日志
--privileged=true \
ponylee/centos7-nginx:latest

访问

curl http://127.0.0.1:8088/hello
curl http://127.0.0.1:8088/verify

nginx reload

docker exec -it ngx_lua nginx -s reload

相关内容

热门资讯

LinkedList源码解析 Java源码系列:下方连接 http://t.csdn.cn/Nwzed 文章目录...
软件测试2 web测试 (1)web控件测试 ​ 界面检查、单行文本框、多行文本框、...
JVM调优策略 对JVM内存的系统级的调优主要的目的是减少GC的频率和Full GC的次数。   1.Full GC...
快速过一遍ThreadLoca... Thread属性之ThreadLocalMapThreadLocal是java的用来做线程隔离的一个...
【云原生-Docker】Doc... 前面大概介绍了下 Docker组成 Docker大部分的操作都围绕着它的三大核心概念:...
Android---动态权限申... 目录 权限分类 动态权限核心函数 简易实现案例 完整代码     Google 在 Android ...
镜像制作dockerfile编... 1.基于容器制作镜像 示例1: step(1)创建容器并编写内容 [root@...
tcp服务器设置accpet为... 监听socket必须绑定一个端口,以便其他客户端可以连接到这个端口,并与...
试题 历届真题 天干地支【第十... 一、试题来源:第十一届蓝桥杯——天干地支 资源限制 内存限制:256.0...
为什么需要在差分或者重要信号换... 大家可能如果对画PCB没有经验的话,可能不太理解为什么差分线在换层时需要在差分孔旁边打...
Linux线程同步 写在前面 来说线程最后一个内容,今天将补充线程互斥的缺陷,同时我们将学习最常见的一个设计模式,最后我...
快速安装TensorFlow2... 该教程仅适用于初学者,用CPU版本的TensorFlow,安装更快更简单...
从入门到精通:识别滑块验证码缺... 验证码识别是目前互联网应用中普遍存在的技术之一,它通过验证用户输入的信息是否符合要求来...
UART使用 目录 一、uart 二、终端 Terminal 1、终端的分类 2、终端对应的设备节点 三、串口的应...
ONLYOFFICE Docs... ONLYOFFICE Docs crack   文档编辑器   增加了对添加复杂字段的支持。   添...
数学建模-如何用matlab画... 1 画图基本指令hold on :保持打开的命令关闭图形保持功能hold off:title ( x...
ROC曲线和AUC值 ROC曲线(Receiver Operating Characteristic...
【2023.3.8】数据结构复... 【2023.3.8】数据结构复习笔记 文章目录【2023.3.8】数据结构复习笔记序言一、绪论二、线...
一个完整的渗透学习路线是怎样的... 前言 1/我是如何学习黑客和渗透? 我是如何学习黑客和渗透测试的,在这里...
HJ27 查找兄弟单词 描述 定义一个单词的“兄弟单词”为:交换该单词字母顺序(注:...