Nginx共11个处理阶段,而相应的处理阶段是能够作插入式处理,便可插拔式架构;另外指令能够在http、server、server if、location、location if几个范围进行配置:html
指令 | 所到处理阶段 | 使用范围 | 解释 |
---|---|---|---|
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进程启动时调用的计时器,若是Master进程不容许则只会在init_by_lua以后调用; 一般用于定时拉取配置/数据,或者后端服务的健康检查 |
set_by_lua set_by_lua_file |
rewrite | server,server if,location,location if |
设置nginx变量,能够实现复杂的赋值逻辑;此处是阻塞的,Lua代码要作到很是快; |
rewrite_by_lua rewrite_by_lua_file |
rewrite tail |
http,server,location,location if |
rrewrite阶段处理,能够实现复杂的转发/重定向逻辑; |
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 |
log阶段处理,好比记录访问量/统计平均响应时间 |
更详细的解释请参考http://wiki.nginx.org/HttpLuaModule#Directives。如上指令不少并不经常使用,所以咱们只拿其中的一部分作演示。nginx
每次Nginx从新加载配置时执行,能够用它来完成一些耗时模块的加载,或者初始化一些全局配置;在Master进程建立Worker进程时,此指令中加载的全局变量会进行Copy-OnWrite,即会复制到全部全局变量到Worker进程。web
#共享全局变量,在全部worker间共享 lua_shared_dict shared_data 1m; init_by_lua_file /usr/example/lua/init.lua;
-- 初始化耗时的模块 local redis = require("resty.redis") local cjson = require("cjson") -- 全局变量,不推荐 count = 1 -- 共享全局内存 local shared_data = ngx.shared.shared_data shared_data:set("count", 1)
count = count + 1 ngx.say("global variable:", count) local shared_data = ngx.shared.shared_data ngx.say(", shared memory : ", shared_data:get("count")) shared_data:incr("count", 1) ngx.say("hello world")
访问如http://127.0.0.1/lua 会发现全局变量一直不变,而共享内存一直递增
global variable : 2 , shared memory : 8 hello world
很不幸,上面只是理论结果,实际本人测试为全局变量会随着刷新而改动,永远比共享内存大1,怀疑nginx新的版本更新了全局变量的定义
另外注意必定在生产环境开启lua_code_cache,不然每一个请求都会建立Lua VM实例。redis
用于启动一些定时任务,好比心跳检查,定时拉取服务器配置等等;此处的任务是跟Worker进程数量有关系的,好比有2个Worker进程那么就会启动两个彻底同样的定时任务。json
init_worker_by_lua /usr/openResty/lua/init_worker.lua;
local count = 0 local delayInSeconds = 3 local heartbeatCheck = nil heartbeatCheck = function ( args ) count = count + 1 ngx.log(ngx.ERR, "do check ", count) local ok, err = ngx.timer.at(delayInSeconds, heartbeatCheck) if not ok then ngx.log(ngx.ERR, "failed to startup heartbeart worker...", err) end end heartbeatCheck()
ngx.timer.at:延时调用相应的回调方法;ngx.timer.at(秒单位延时,回调函数,回调函数的参数列表);能够将延时设置为0即获得一个当即执行的任务,任务不会在当前请求中执行不会阻塞当前请求,而是在一个轻量级线程中执行。
另外根据实际状况设置以下指令
lua_max_pending_timers 1024; #最大等待任务数
lua_max_running_timers 256; #最大同时运行任务数后端
设置nginx变量,咱们用的set指令即便配合if指令也很难实现负责的赋值逻辑;服务器
location /lua_set_1 { default_type "text/html"; set_by_lua_file $num /usr/openResty/lua/test_set_1.lua; echo $num; }
set_by_lua_file:语法set_by_lua_file $var lua_file arg1 arg2…; 在lua代码中能够实现全部复杂的逻辑,可是要执行速度很快,不要阻塞;cookie
local uri_args = ngx.req.get_uri_args() local i = uri_args["i"] or 0 local j = uri_args["j"] or 0 return i + j
获得请求参数进行相加而后返回。
访问如http://127.0.0.1/lua_set_1?i=1&j=10进行测试。 若是咱们用纯set指令是没法实现的。
再举个实际例子,咱们实际工做时常常涉及到网站改版,有时候须要新老并存,或者切一部分流量到新版
2.1 在openResty.conf中使用map指令来映射host到指定nginx变量,方便咱们测试架构
############ 测试时使用的动态请求 map $host $item_dynamic { default "0"; item2014.jd.com "1"; }
如绑定hosts
192.168.1.2 item.jd.com;
192.168.1.2 item2014.jd.com;
此时咱们想访问item2014.jd.com时访问新版,那么咱们能够简单的使用如svg
if ($item_dynamic = "1") { proxy_pass http://new; } proxy_pass http://old;
可是咱们想把商品编号为为8位(好比品类为图书的)没有改版完成,须要按照相应规则跳转到老版,可是其余的到新版;虽然使用if指令能实现,可是比较麻烦,基本须要这样
set jump "0"; if($item_dynamic = "1") { set $jump "1"; } if(uri ~ "^/6[0-9]{7}.html") { set $jump "${jump}2"; } #非强制访问新版,且访问指定范围的商品 if (jump == "02") { proxy_pass http://old; } proxy_pass http://new;
以上规则仍是比较简单的,若是涉及到更复杂的多重if/else或嵌套if/else实现起来就更痛苦了,可能须要到后端去作了;此时咱们就能够借助lua了:
set_by_lua $to_book ' local ngx_match = ngx.re.match local var = ngx.var local skuId = var.skuId local r = var.item_dynamic ~= "1" and ngx.re.match(skuId, "^[0-9]{8}$") if r then return "1" else return "0" end; '; set_by_lua $to_mvd ' local ngx_match = ngx.re.match local var = ngx.var local skuId = var.skuId local r = var.item_dynamic ~= "1" and ngx.re.match(skuId, "^[0-9]{9}$") if r then return "1" else return "0" end; '; #自营图书 if ($to_book) { proxy_pass http://127.0.0.1/old_book/$skuId.html; } #自营音像 if ($to_mvd) { proxy_pass http://127.0.0.1/old_mvd/$skuId.html; } #默认 proxy_pass http://127.0.0.1/proxy/$skuId.html;
执行内部URL重写或者外部重定向,典型的如伪静态化的URL重写。其默认执行在rewrite处理阶段的最后。
location /lua_rewrite_1 { default_type "text/html"; rewrite_by_lua_file /usr/openResty/lua/test_rewrite_1.lua; echo "no rewrite"; }
if ngx.req.get_uri_args()["jump"] == "1" then return ngx.redirect("http://www.baidu.com?jump=1", 302) end
当咱们请求http://127.0.0.1/lua_rewrite_1时发现没有跳转,而请求http://127.0.0.1/lua_rewrite_1?jump=1时发现跳转到百度首页了。 此处须要301/302跳转根据本身需求定义。
location /lua_rewrite_2 { default_type "text/html"; rewrite_by_lua_file /usr/openResty/lua/test_rewrite_2.lua; echo "no rewrite"; }
if ngx.req.get_uri_args()["jump"] == "1" then ngx.req.set_uri("/lua_rewrite_3", false); ngx.req.set_uri("/lua_rewrite_4", false); ngx.req.set_uri_args({a = 1, b = 2}); end
ngx.req.set_uri(uri, false):能够内部重写uri(能够带参数),等价于 rewrite ^ /lua_rewrite_3;经过配合if/else能够实现 rewrite ^ /lua_rewrite_3 break;这种功能;此处二者都是location内部url重写,不会从新发起新的location匹配;
ngx.req.set_uri_args:重写请求参数,能够是字符串(a=1&b=2)也能够是table;
访问如http://127.0.0.1/lua_rewrite_2?jump=0时获得响应
rewrite2 uri : /lua_rewrite_2, a :
访问如http://127.0.0.1/lua_rewrite_2?jump=1时获得响应
rewrite2 uri : /lua_rewrite_4, a : 1
location /lua_rewrite_3 { default_type "text/html"; rewrite_by_lua_file /usr/openResty/lua/test_rewrite_3.lua; echo "no rewrite"; }
if ngx.req.get_uri_args()["jump"] == "1" then ngx.req.set_uri("/lua_rewrite_4", true); ngx.log(ngx.ERR, "=========") ngx.req.set_uri_args({a = 1, b = 2}); end
ngx.req.set_uri(uri, true):能够内部重写uri,即会发起新的匹配location请求,等价于 rewrite ^ /lua_rewrite_4 last;此处看error log是看不到咱们记录的log。
因此请求如http://127.0.0.1/lua_rewrite_3?jump=1会到新的location中获得响应,此处没有/lua_rewrite_4,因此匹配到/lua请求,获得相似以下的响应
global variable : 2 , shared memory : 1 hello world
即
rewrite ^ /lua_rewrite_3; 等价于 ngx.req.set_uri("/lua_rewrite_3", false);
rewrite ^ /lua_rewrite_3 break; 等价于 ngx.req.set_uri("/lua_rewrite_3", false); 加 if/else判断/break/return
rewrite ^ /lua_rewrite_4 last; 等价于 ngx.req.set_uri("/lua_rewrite_4", true);
注意,在使用rewrite_by_lua时,开启rewrite_log on;后也看不到相应的rewrite log。
用于访问控制,好比咱们只容许内网ip访问,可使用以下形式
allow 127.0.0.1; allow 10.0.0.0/8; allow 192.168.0.0/16; allow 172.16.0.0/12; deny all;
location /lua_access { default_type "text/html"; rewrite_by_lua_file /usr/openResty/lua/test_access.lua; echo "access"; }
if ngx.req.get_uri_args()["token"] ~= "123" then return ngx.exit(403) end
即若是访问如http://17.0.0.12/lua_access?token=234将获得403 Forbidden的响应。这样咱们能够根据如cookie/用户token来决定是否有访问权限。
另外在使用PCRE进行正则匹配时须要注意正则的写法,具体规则请参考http://wiki.nginx.org/HttpLuaModule中的Special PCRE Sequences部分。还有其余的注意事项也请阅读官方文档。