nginx lua模块常用的指令
lua_code_cache
语法:lua_code_cache on | off
默认: on
适用上下文:http、server、location、location if
这个指令是指定是否开启lua的代码编译缓存,开发时可以设置为off,以便lua文件实时生效,如果是生产线上,为了性能,建议开启。
lua_package_path
语法:lua_package_path <lua-style-path-str>
默认:由lua的环境变量决定
适用上下文:http
设置lua代码的寻找目录。
例如:lua_package_path "/opt/nginx/conf/www/?.lua;;";
具体的路径设置要参考lua的模块机制
init_by_lua(_file)
语法:init_by_lua <lua-script-str>
适用上下文:
http
init_by_lua 'cjson = require "cjson"';
server {
location = /api {
content_by_lua '
ngx.say(cjson.encode({dog = 5, cat = 6}))
';
}
}
从这段配置代码,我们可以看出,其实这个指令就是初始化一些lua的全局变量,以便后续的代码使用。
注:有(_file)的选项代表可以直接引用外部的lua源代码文件,效果与直接写配置文件一样,不过可维护性当然是分开好点。
init_worker_by_lua(_file)
类似于上面的,不过是作用在work进程的,先于work进程启动而调用。
set_by_lua(_file)
语法:set_by_lua $res <lua-script-str> [$arg1 $arg2 ...]
适用上下文:server、location、location if
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";
}
这个指令是为了能够让nginx的变量与lua的变量相互作用赋值
content_by_lua(_file)
语法:content_by_lua <lua-script-str>
适用上下文:location、location if
location /nginx_var {
# MIME type determined by default_type:
default_type 'text/plain';
# try access /nginx_var?a=hello,world
content_by_lua "ngx.print(ngx.var['arg_a'], '\\n')";
}
通过这个指令,可以由lua直接确定nginx响应页面的正文。
rewrite_by_lua(_file)
语法:rewrite_by_lua <lua-script-str>
适用上下文:location、location if
这个指令更多的是为了替代HttpRewriteModule的rewrite指令来使用的,优先级低于rewrite指令
比如
location /foo {
set $a 12; # create and initialize $a
set $b ''; # create and initialize $b
rewrite_by_lua 'ngx.var.b = tonumber(ngx.var.a) + 1';
if ($b = '13') {
rewrite ^ /bar redirect;
break;
}
echo "res = $b";
}
这个并不会像预期的那样子,因为我猜测,rewrite_by_lua是开启一个协程去工作的,可是下面却继续执行下去了,所以得不到预期的结果。
此时如果由lua代码来控制rewrite,那就没有问题了。
location /foo {
set $a 12; # create and initialize $a
set $b ''; # create and initialize $b
rewrite_by_lua '
ngx.var.b = tonumber(ngx.var.a) + 1
if tonumber(ngx.var.b) == 13 then
return ngx.redirect("/bar");
end
';
echo "res = $b";
}
access_by_lua(_file)
语法:access_by_lua <lua-script-str>
适用上下文:http, server, location, location if
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/...
}
顾名思义,这个指令用在验证通过或者需要验证的时候。
header_filter_by_lua(_file)
语法:header_filter_by_lua <lua-script-str>
适用上下文:http, server, location, location if
location / {
proxy_pass http://mybackend;
header_filter_by_lua 'ngx.header.Foo = "blah"';
}
用lua的代码去指定http响应的 header一些内容。
body_filter_by_lua(_file)
语法:body_filter_by_lua <lua-script-str>
适用上下文:http, server, location, location if
location /t {
echo hello world;
echo hiya globe;
body_filter_by_lua '
local chunk = ngx.arg[1]
if string.match(chunk, "hello") then
ngx.arg[2] = true -- new eof
return
end
-- just throw away any remaining chunk data
ngx.arg[1] = nil
';
}
这个指令可以用来篡改http的响应正文的。
转:https://blog.csdn.net/xiaofei0859/article/details/76864544
相关推荐:
nginx lua模块常用的指令的更多相关文章
- mac下Nginx+lua模块编译安装
Nginx的nb之处就不说了,lua也是一个小巧的脚本语言,由标准C编写而成,几乎可以运行在所有的平台上,也非常强大,其他特性请自行度娘.nginx_lua_module是由淘宝的工程师清无(王晓哲) ...
- Nginx主要模块常用指令说明
核心模块(Core Modules): 主模块(Main Module):配置和服务器全局有关的一些参数,比如错误日志.进程.权限等 user worker_processes error_logsy ...
- nginx核心模块常用指令
默认启动Nginx时,使用的配置文件是: 安装路径/conf/nginx.conf 文件,可以在启动nginx的时候,通过-c来指定要读取的配置文件 常见的配置文件有如下几个: nginx.conf: ...
- nginx lua模块安装
1.安装LuaJIT,LuaJIT为LuaJIT is a Just-In-Time Compiler (JIT) for the Lua programming language wget http ...
- OpenResty(Nginx + Lua)常用操作
平滑重启: /usr/servers/nginx/sbin/nginx -s reload 重启前测试配置是否成功 /usr/servers/nginx/sbin/nginx -t 平滑升级如果服 ...
- 高并发 Nginx+Lua OpenResty系列(3)——模块指令
Nginx Lua 模块指令 Nginx共11个处理阶段,而相应的处理阶段是可以做插入式处理,即可插拔式架构:另外指令可以在http.server.server if.location.locatio ...
- 用Nginx+Lua(OpenResty)开发高性能Web应用
在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开 ...
- Nginx+Lua(OpenResty)开发高性能Web应用
使用Nginx+Lua(OpenResty)开发高性能Web应用 博客分类: 跟我学Nginx+Lua开发 架构 ngx_luaopenresty 在互联网公司,Nginx可以说是标配组件,但是主要场 ...
- 使用Nginx+Lua(OpenResty)开发高性能Web应用
摘自(http://jinnianshilongnian.iteye.com/blog/2280928) 在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等 ...
随机推荐
- attachEvent
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- opencv结构IplImage
转载请注明来源:https://www.cnblogs.com/hookjc/ typedef struct _IplImage{int nSize; /* Ip ...
- redis lua脚本学习
语法格式(常见) a = 5 -- 全局变量 local b = 5 -- 局部变量 Eval的使用 EVAL script numkeys key [key ...] arg [arg ...] 首 ...
- CSS网页使用Font Awesome图标字体时,css定义 content 属性
原文地址: http://blog.csdn.net/laurel_y/article/details/70842157
- Python概述 —变量及运算符
Python概述-变量及运算符 1.变量的构成 2.变量的类型 3.内存模型 4.变量命名规则 5. 算数与逻辑运算符 6.位运算符 #变量的构成 变量名:方便查找 变量值:实际要存储的内容 变量类型 ...
- 集合、Collection、list、set、HashSet
一.集合的理解:将多个数据放在一起 简介: 1).可以动态保存任意多个对象,使用比较方便!2).提供了一系列方便的操作对象的方法: add.remove.set. get等 1.集合中的实现和继承图 ...
- SQL性能优化技巧
作者:IT王小二 博客:https://itwxe.com 这里就给小伙伴们带来工作中常用的一些 SQL 性能优化技巧总结,包括常见优化十经验.order by 与 group by 优化.分页查询优 ...
- Solution -「SDOI 2018」「洛谷 P4606」战略游戏
\(\mathcal{Description}\) Link. 给定一个 \(n\) 个点 \(m\) 条边的无向连通图,\(q\) 次询问,每次给出一个点集 \(s\),求至少在原图中删去多 ...
- 数据缓存Cache
在MyBatis - 随笔分类 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中有关于Mybatis中Cache技术实现及应用介绍.Cache技术实现都是implements Cache ...
- react 也就这么回事 01 —— React 元素的创建和渲染
React 是一个用于构建用户界面的 JavaScript 库 它包括两个库:react.js 和 react-dom.js react.js:React 的核心库,提供了 React.js 的核心功 ...