Lua+Nginx的执行顺序
来源:互联网 发布:二叉树 java 编辑:程序博客网 时间:2024/06/04 19:50
Lua执行步骤
init_by_lua/init_by_lua_file:在nginx初始化配置的时候,运行该lua脚本,常用语初始化全局变量一些长连接的初始化,一般存放在这个位置,或者共享内存的初始化,只有nginx重启后,共享内存数据才会清空 lua_shared_dict dogs 1m; init_by_lua ' local dogs = ngx.shared.dogs; dogs:set("Tom", 56) '; server { location = /api { content_by_lua ' local dogs = ngx.shared.dogs; ngx.say(dogs:get("Tom")) '; } }
set_by_lua/set_by_lua_file:设置一个变量,常用于计算一个逻辑,然后返回结果另外可以将已有的nginx变量当做参数传递到Lua脚本里 location /foo { set $diff ''; # we have to predefine the $diff variable here set_by_lua $sum ' local a = 32 local b = 56 ngx.var.diff = a - b; -- write to $diff directly return a + b; -- return the $sum value normally '; echo "sum = $sum, diff = $diff"; }
rewrite_by_lua/rewrite_by_lua_file:执行内部URL重写或者外部重定向,默认执行在rewrite处理阶段的最后 location = /check-spam { internal; proxy_pass http://foo.com/check-spam; } location / { rewrite_by_lua ' local res = ngx.location.capture("/check-spam") if res.body == "spam" then return ngx.redirect("/terms-of-use.html") end '; fastcgi_pass ...; }
access_by_lua/access_by_lua_file:主要用于访问控制,如只允许内网IP才能访问,能收集到大部分变量,类似status需要在log阶段才有 location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; deny all; access_by_lua ' local res = ngx.location.capture("/mysql", { ... }) ... '; # proxy_pass/fastcgi_pass/... }
content_by_lua/content_by_lua_file:该阶段是所有请求处理阶段中最为重要的一个,运行在这个阶段的脚本一般都用于生成内容并输出HTTP相应 location /other { set $dog "$dog world"; echo "$uri dog: $dog"; } location /lua { set $dog 'hello'; content_by_lua ' res = ngx.location.capture("/other", { copy_all_vars = true }); ngx.print(res.body) ngx.say(ngx.var.uri, ": ", ngx.var.dog) '; }Request GET /lua will give the output/other dog: hello world/lua: hello
header_filter_by_lua/header_filter_by_lua_file:一般只用于设置cookie和header等
body_filter_by_lua/body_filter_by_lua_file:一般会在一次请求中被调用多次
log_by_lua/log_by_lua_file:该阶段运行在请求结束的时候,用于请求的后续操作 lua_shared_dict log_dict 5M; server { location / { proxy_pass http://mybackend; log_by_lua ' local log_dict = ngx.shared.log_dict local upstream_time = tonumber(ngx.var.upstream_response_time) local sum = log_dict:get("upstream_time-sum") or 0 sum = sum + upstream_time log_dict:set("upstream_time-sum", sum) local newval, err = log_dict:incr("upstream_time-nb", 1) if not newval and err == "not found" then log_dict:add("upstream_time-nb", 0) log_dict:incr("upstream_time-nb", 1) end '; } location = /status { content_by_lua ' local log_dict = ngx.shared.log_dict local sum = log_dict:get("upstream_time-sum") local nb = log_dict:get("upstream_time-nb") if nb and sum then ngx.say("average upstream response time: ", sum / nb, " (", nb, " reqs)") else ngx.say("no data yet") end '; } }
Nginx执行步骤(共十一个步骤)
post-read:读取请求内容阶段Nginx读取并解析完请求头之后就立即开始运行
server-rewriteServer请求地址重写阶段
find-config配置查找阶段
rewriteLocation请求地址重写阶段
post-rewrite请求地址重写提交阶段
preaccess访问权限检查准备阶段
access访问权限检查阶段
post-access访问权限检查提交阶段
try-files配置项try_files处理阶段
content内容产生阶段
log日志模块处理阶段记录日志
0 0
- Lua+Nginx的执行顺序
- Nginx与Lua执行顺序
- nginx与lua的执行顺序和步骤说明
- nginx与lua的执行顺序和步骤说明
- Nginx-Lua模块的执行顺序及指令
- linux中Nginx与Lua执行顺序详解
- lua源代码执行顺序
- Nginx 配置指令的执行顺序
- nginx location 执行顺序
- cocos2dx lua的require、读取和执行顺序
- Nginx 配置指令的执行顺序(八)
- nginx配置指令的执行顺序(1)转载子章亦春
- Nginx,Lua的结合
- nginx+lua的函数
- lua代码的执行
- Nginx 关于 Rewrite 执行顺序详解
- Nginx 关于 Rewrite 执行顺序详解
- Nginx 关于 Rewrite 执行顺序详解
- css中单位px和em,rem的区别
- 30.ajax路由分离模块式开发步骤
- 微型投影仪第四篇——Windows无屏PC
- 解决WeX5中IOS设备应用标题栏被系统状态栏覆盖的BUG
- ubuntu gedit 中文
- Lua+Nginx的执行顺序
- 学习GreenDao路上的经验总结
- sparklyr — R interface for Apache Spark
- @ResponseBody和@ResquestBody注解的作用
- 8086CPU汇编loop指令和cx寄存器
- Solaris下Cron相关信息
- myeclipse下面的Web项目的项目名和部署到tomcat的名字不一样。
- MVC的简单介绍及应用(一)
- Linux用户管理