环境
系统: CentOS 7.2
安装
安装依赖
yum install pcre-devel openssl-devel gcc curl
准备nginx_openresty
下载nginx_openresty并解压
wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
tar -xzvf ngx_openresty-1.7.7.2.tar.gz
在ngx_openresty-1.7.7.2/bundle目录存放nginx核心和一些第三方模块
安装LuaJIT
cd ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-20150120/
make clean && make && make install
ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit
准备常用模块
ngx_cache_purge模块用于清理nginx缓存
cd ngx_openresty-1.7.7.2/bundle
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -xvf 2.3.tar.gz
nginx_upstream_check_module模块用于upstream健康检查
cd ngx_openresty-1.7.7.2/bundle
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -xvf v0.3.0.tar.gz
安装ngx_openresty及其模块
cd /usr/servers/ngx_openresty-1.7.7.2
./configure --prefix=/usr/servers \
--with-http_realip_module \
--with-pcre \
--with-luajit \
--add-module=./bundle/ngx_cache_purge-2.3/ \
--add-module=./bundle/nginx_upstream_check_module-0.3.0/ \
-j2
make && make install
配置项简单说明
| 配置项 | 配置说明 |
|---|---|
| --with-xx | 内置模块/集成模块 |
| --add-module | 添加自定义的第三方模块 |
安装完毕,会多出一些目录:
- nginx
- luajit
- lualib
启动nginx
cd nginx
./sbin/nginx
配置
配置nginx.conf,在http部分增加lua模块路径
#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
lua_package_path "/usr/servers/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/servers/lualib/?.so;;"; #c模块
在nginx/conf目录创建文件lua.conf
#lua.conf
server {
listen 80;
server_name _;
}
在nginx.conf的http部分增加
include lua.conf;
测试配置是否正常
nginx -t
简单示例:Hello World
在lua.conf增加location
location /lua {
default_type 'text/html';
content_by_lua 'ngx.say("hello world")';
}
检测配置是否正确,重启nginx服务
nginx -s reload
注意nginx配置对于空格,字符编码的严格要求
lua文件单独维护
之前lua只是lua.conf文件一部分,现在考虑单独维护lua文件
创建lua文件conf/lua/test.lua
ngx.say("hello nginx");
修改lua.conf
content_by_lua_file conf/lua/test.lua;
lua_code_cache
lua_code_cache默认开启,也就是缓存lua代码,因此当lua代码改动时,需要通过重启nginx才能生效。在开发阶段可以关闭lua_code_cache,但是正式环境则必须开启
location /lua {
...
lua_code_cache off;
...
}
日志
如果在运行时出现错误,可以查看错误日志
tail -f nginx/logs/error.log
nginx+lua项目
上面的简单示例仅仅是为了熟悉nginx与lua的关系,作为一个lua项目还远远不够。
lua的项目结构大致如下
- example
- example.conf
- lua
- test1.lua
- test2.lua
- lualib
- *.lua
- *.so
此时nginx.conf则是最小化的配置
#user nobody;
worker_processes 2;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type text/html;
#lua模块路径,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
lua_package_path "/usr/example/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/example/lualib/?.so;;"; #c模块
include /usr/example/example.conf;
}
项目的配置文件example.conf
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/example/lua/test.lua;
}
}
Nginx Lua API
请求
ngx.var:nginx变量ngx.req.get_headers:默认只获取前100ngx.req.get_uri_args:获取url请求参数ngx.req.get_post_args:获取post请求体ngx.req.raw_header:未解析的请求头字符串ngx.req.get_body_data:未解析的请求体body内容字符串
输出
ngx.header:输出响应头ngx.print:输出响应体ngx.say:同ngx.print,最后输出一个换行符ngx.exit:指定状态码并退出ngx.redirect:重定向
编码/解码
ngx.escape_uri/ngx.unescape_uri:uri编码/解码ngx.encode_args/ngx.decode_args:参数编码/解码ngx.encode_base64/ngx.decode_base64:BASE64编码/解码ngx.re.match:nginx正则表达式匹配
Nginx全局内存
设置全局共享变量
lua shared_dict shared_data 1m;
获取全局共享变量
local shared_data=ngx.shared.shared_data
Nginx Lua模块指令
| 指令 | 阶段 | 范围 | 说明 |
|---|---|---|---|
| init_by_lua/init_by_lua_file | loading-config | http | nginx master进程加载配置时执行,通常用于初始化全局配置,预加载Lua模块 |
| init_worker_by_lua/init_worker_by_lua_file | starting-worker | http | nginx worker启动时,通常用于定时拉取配置数据,或后端服务健康检查 |
| set_by_lua/set_by_lua_file | rewrite | server,server if, location, location if | 设置nginx变量,实现复杂赋值逻辑 |
| rewrite_by_lua/rewrite_by_lua_file | rewrite tail | http, server, location, location if | 实现复杂转发/重定向逻辑 |
| access_by_lua/access_by_lua_file | access tail | http, server,location, location if | 请求访问阶段,用于访问控制 |
| content_by_lua/content_by_lua_file | content | location, location if | 内容处理,接收请求并输出响应 |
| header_filter_by_lua/header_filter_by_lua_file | output-header-filter | http, server, location ,location if | 设置header和cookie |
| body_filter_by_lua/body_filter_by_lua_file | output-body-filter | http, server,location, location if | 对响应数据过滤,比如阶段或替换 |
| log_by_lua/log_by_lua_file | log | http, server, location, location if | 记录访问量/平均响应时间 |