openresty入门
搭建
安装依赖
pcre-devel openssl-devel gcc curl
下载安装包
官网:
https://openresty.org/en/download.html
下载最新版
wget https://openresty.org/download/openresty-1.19.9.1.tar.gz
tar -zxvf openresty-1.19.9.1.tar.gz
cd openresty-1.19.9.1/
./configure
sudo make -j6
sudo make install
要是报
gmake[1]: cc: Command not found
则是没有cc指令(其实就是gcc)
可以先看看gcc的位置,再创建软链接
gccPath=$(which gcc)
ln -sf $gccPath /bin/cc
再添加环境变量,以及赋权
echo 'export PATH=$PATH:/usr/local/openresty/bin' >> ~/.bashrc
sudo chmod +s /usr/local/openresty/bin/openresty
source ~/.bashrc
开始运行
openresty
nginx 与 lua
参考 https://cloud.tencent.com/developer/article/1037840
https://blog.openresty.com.cn/cn/or-lua-module/
Nginx配置
假设Nginx相关的配置如下所示
lua_code_cache off;
location ~ ^/api/([-_a-zA-Z0-9/]+) {
content_by_lua_file lua/$1.lua;
}
当来到的请求符合 ^/api/([-_a-zA-Z0-9/]+) 时,会在NGX_HTTP_CONTENT_PHASE HTTP请求内容阶段交给 lua/$1.lua来处理
比如
/api/addition 交给 lua/addition.lua 处理
/api/substraction 交给 lua/substraction.lua 处理
请求的处理
content_by_lua_file对应的请求来临时,执行流程为
ngx_http_lua_content_handler -> ngx_http_lua_content_handler_file-> ngx_http_lua_content_by_chunk