angie nginx_lua 模块使用
angie 包含了不少社区模块,docker 镜像更加方便,都已经安装了,以下是一个测试lua 模式的使用
因为默认官方构建的是一个动态扩展模块,对于三方lua 模块安装不是很方便,我参考了openresty
对于luarocks 的支持,这样我们自己安装三方lua 模块就比较方便的
构建支持luarocks 的dcoker 镜像
为了方便我是直接基于docker 版本的rocky 镜像修改的,直接通过docker 构建已经安装luarocks,其他系统的类似
- Dcokerfile
基于luarocks 构建部分我直接复制了openresty 的luajit 目录的文件
FROM docker.angie.software/angie:1.3.1-rocky
RUN mkdir -p /run/angie/
# copy from openresty add cjson
COPY openresy-lua/luajit/ /usr/share/angie/lualib/
COPY luarocks-3.9.2.tar.gz /tmp/luarocks-3.9.2.tar.gz
RUN yum install -y wget gcc unzip \
&& cd /tmp/ && tar -zxvf luarocks-3.9.2.tar.gz \
&& cd luarocks-3.9.2 \
&& ./configure --prefix=/usr/share/angie/lualib/ \
--with-lua=/usr/share/angie/lualib/ \
--lua-suffix=jit \
--with-lua-include=/usr/share/angie/lualib/include/luajit-2.1 \
&& make build \
&& make install \
&& cd /tmp \
&& rm -rf luarocks-3.9.2.tar.gz luarocks-3.9.2 \
&& ln -s /usr/share/angie/lualib/share/lua/5.1 /usr/share/lua/5.1
# 此处方便找一些c库依赖
ENV LUA_CPATH="/usr/share/angie/lualib/?.so;/usr/share/angie/?.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/share/angie/lualib/lib/?.so;/usr/local/lib/lua/5.1/loadall.so;/usr/share/angie/lualib/lib/lua/5.1/?.so"
lua 模块开发服务
- 应用Dockerfile
FROM dalongrong/angie:1.3.1-rocky-luarocks
# 安装了hashids 以及lua-resty-http,基于上边构建好的luarocks
RUN /usr/share/angie/lualib/bin/luarocks install lua-resty-hashids \
&& /usr/share/angie/lualib/bin/luarocks install lua-resty-http
- 开启lua 模块
angie.conf
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
- location 集成
angie 默认的default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/angie/html;
index index.html index.htm;
}
location /status/ {
api /status/;
allow 127.0.0.1;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/angie/html;
}
# cjson hashids 测试
location /demo {
default_type text/html;
content_by_lua_block {
local cjson = require("cjson")
local hashids = require("hashids");
local h = hashids.new("this is my salt")
local hash = h:encode(1337)
local conf = {
name = "dalong",
age =333,
id = hash,
}
ngx.say(cjson.encode(conf))
}
}
# http 请求测试
location = /http {
default_type text/html;
content_by_lua_block {
local httpc = require("resty.http").new()
-- Single-shot requests use the `request_uri` interface.
local res, err = httpc:request_uri("http://192.168.254.241:8080/app.json", {
method = "GET"
})
if not res then
ngx.log(ngx.ERR, "request failed: ", err)
return
end
-- At this point, the entire request / response is complete and the connection
-- will be closed or back on the connection pool.
-- The `res` table contains the expeected `status`, `headers` and `body` fields.
local status = res.status
local length = res.headers["Content-Length"]
local body = res.body
ngx.say(body)
}
}
}
- 效果
说明
构建好的镜像我已经push dockerhub 了,可以直接使用dalongrong/angie:1.3.1-rocky-luarocks
, 目前测试上包查找以及模块功能上与openresty
基本类似,angie 的all-in-one 模式还是挺方便的
参考资料
https://angie.software/en/install/#install-docker
https://github.com/openresty/docker-openresty/blob/master/alpine/Dockerfile.fat
https://github.com/luarocks/luarocks
https://github.com/luarocks/luarocks/wiki/Installation-instructions-for-Unix