HttpLuaModule——翻译(一)
最近经常使用春哥和小哲老师写的NGINX-LUA,非常苦于没有中文文档,特别是向我这种英文水平实在有限的同学,所以将遇到的模块记录下来,供以后参考!原文:http://wiki.nginx.org/HttpLuaModule
lua_code_cache
一般放在nginx.conf里面,设置lua程序是否缓存,默认是开启的,开发模式开启即可:lua_code_cache off。开启后,重启nginx会有提示:nginx: [warn] lua_code_cache is off; this will hurt performance in /home/wb-liqiu/git/dante/conf/nginx.conf:30
lua_regex_cache_max_entries
lua_package_path
设置lua的包含路径,例如:lua_package_path '/home/wb-liqiu/git/dante/lib/?.lua;/home/lz/luax/?.lua;;';
lua_package_cpath
顾名思义,设置lua的C扩展的路径,例如:lua_package_cpath '/home/lz/luax/?.so;;';
init_by_lua
设置lua的全局变量,在NGINX启动的时候生效。例如:init_by_lua 'cjson = require "cjson"'; 事例:
init_by_lua 'cjson = require "cjson"';
server {
location = /api {
content_by_lua '
ngx.say(cjson.encode({dog = , cat = }))
';
}
}
还可以这么用:
lua_shared_dict dogs 1m;
init_by_lua '
local dogs = ngx.shared.dogs;
dogs:set("Tom", )
';
server {
location = /api {
content_by_lua '
local dogs = ngx.shared.dogs;
ngx.say(dogs:get("Tom"))
';
}
}
init_by_lua_file
同上
set_by_lua
给nginx变量赋值
set_by_lua_file
content_by_lua
执行lua程序,例如:
content_by_lua '
ngx.print("test")
';
content_by_lua_file
同上
rewrite_by_lua
这个模块经常在标准的HttpRewriteModule模块之后执行。例如:
location /foo {
set $a ; # create and initialize $a
set $b ""; # create and initialize $b
rewrite_by_lua 'ngx.var.b = tonumber(ngx.var.a) + 1';
echo "res = $b";
}
执行之后,$b是13,当然如下语句就不能执行
location /foo {
set $a ; # create and initialize $a
set $b ''; # create and initialize $b
rewrite_by_lua 'ngx.var.b = tonumber(ngx.var.a) + 1';
if ($b = '') {
rewrite ^ /bar redirect;
break;
}
echo "res = $b";
}
ngx_eval 模块类似于 rewrite_by_lua模块。例如:
location / {
eval $res {
proxy_pass http://foo.com/check-spam;
}
if ($res = 'spam') {
rewrite ^ /terms-of-use.html redirect;
}
fastcgi_pass ...;
}
可以这么用:
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 ...;
}
rewrite_by_lua在write模块之后执行(除非rewrite_by_lua_no_postpone 设置为 on),例如:
location /foo {
rewrite ^ /bar;
rewrite_by_lua 'ngx.exit(503)';
}
location /bar {
...
}
rewrite_by_lua_file
同上
HttpLuaModule——翻译(一)的更多相关文章
- HttpLuaModule——翻译(Nginx API for Lua) (转)
现在我已经将翻译的内容放到:http://wiki.nginx.org/HttpLuaModuleZh Nginx API for Lua Introduction 各种各样的*_by_lua和*_b ...
- HttpLuaModule——翻译(Nginx API for Lua)
现在我已经将翻译的内容放到:http://wiki.nginx.org/HttpLuaModuleZh Nginx API for Lua Introduction 各种各样的*_by_lua和*_b ...
- HttpLuaModule——翻译(二)
access_by_lua access阶段.事例: location / { deny 192.168.1.1; allow ; allow ; deny all; access_by_lua ' ...
- 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- 【探索】机器指令翻译成 JavaScript
前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...
- 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...
- 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...
随机推荐
- C#编程(六十一)------------LINQ中的扩展方法
原文链接: http://blog.csdn.net/shanyongxu/article/details/47208401 LINQ中的扩展方法 LINQ中where扩展方法,要想使用,必须导入us ...
- 【pycharm】在pycharm上,使用python的pip安装tensorflow过程
如题:在pycharm上,使用python的pip安装tensorflow过程 最后成功安装的版本信息是: python版本是3.6.5 pip版本是9.0.1 pycharm版本是2018.1 te ...
- 商业web漏扫神器——appscan篇
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dongfei2033/article/details/78472507 很快,已经到了三大商业漏 ...
- 分布式系统:CAP
一致不太理解CAP,最近好像有点感觉了,这里写下来,先介绍下CAP的定义: C:一致性.写完数据后,立马能看到最新数据. A:可用性.所有请求必须有响应. P:分区容错性.网络或服务器故障不会导致系统 ...
- Java ArrayList、string、string[]之间的转换
1.ArrarList 转换为 string[] : ArrayList list = new ArrayList(); list.Add("aaa"); list.Add(&qu ...
- mysql error You must reset your password using ALTER USER statement before executing this statement.
mysql修改密码Your password does not satisfy the current policy requirements 出现这个问题的原因是:密码过于简单.刚安装的mysql的 ...
- script标签加载顺序(defer & async)
script 拥有的属性 async:可选,表示应该立即下载脚本,但不应妨碍页面中的其他操作,比如下载其他资源或等待加载其他脚本.只对外部脚本文件有效. charset:可选.表示通过 src 属性指 ...
- 自动移动的ImageView
图片会慢慢的向左移动,到头了后,再循环 其实这个效果和屏幕背景图片的效果差不多,屏幕背景图是随着滑动来慢慢的滚动,这是自己每个n秒开始动.实现方式自然是用自定的控件了.这次继承的是ImageView ...
- [Android Pro] AndroidStudio IDE界面插件开发(Hello World篇)
转载请注明出处:[huachao1001的专栏:http://blog.csdn.net/huachao1001/article/details/53856916] 工欲善其事必先利其器,自打从Ecl ...
- emouse思·睿—评论与观点整理之三
虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...