最近经常使用春哥和小哲老师写的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——翻译(一)的更多相关文章

  1. HttpLuaModule——翻译(Nginx API for Lua) (转)

    现在我已经将翻译的内容放到:http://wiki.nginx.org/HttpLuaModuleZh Nginx API for Lua Introduction 各种各样的*_by_lua和*_b ...

  2. HttpLuaModule——翻译(Nginx API for Lua)

    现在我已经将翻译的内容放到:http://wiki.nginx.org/HttpLuaModuleZh Nginx API for Lua Introduction 各种各样的*_by_lua和*_b ...

  3. HttpLuaModule——翻译(二)

    access_by_lua access阶段.事例: location / { deny 192.168.1.1; allow ; allow ; deny all; access_by_lua ' ...

  4. 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...

  5. 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...

  6. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  7. 【探索】机器指令翻译成 JavaScript

    前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...

  8. 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...

  9. 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...

随机推荐

  1. “finally block does not complete normally”的警告解决

    但是,java里面不是可以保证finally一定会执行的么,为什么不可以在finally块做return??? 细细看道来: debug一下这个函数,就会惊讶的发现, 里面抛出的异常会被finally ...

  2. 盾牌第一至七季/全集The Shield迅雷下载

    英文译名The Shield,第1-7季(2002-2008)FX.本季看点:<盾牌>一部极具争议性的连续剧,打破了传统警匪片套路,刻画了性格复杂的警察,他们在与各种罪案做斗争的同时,也面 ...

  3. 详细解读Android中的搜索框(二)—— Search Dialog

    Search Dialog是提供搜索的控件之一,还有一个是上次小例子给出的searchView,关于SearchView的东西后面会说到.本次先从Search Dialog说起,让大家慢慢理解andr ...

  4. 开源项目DataTimePicker实现时间和日期的选择

       这个开源项目是模仿Google官方的time选择器做的,是否漂亮.让我爱不释手,真心喜欢.很有幸和大家一起分享下,那么话不多说开始讲解. 开源项目地址:https://github.com/fl ...

  5. jQuery的deferred对象详解 jquery回调函数

    http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html jQuery的 ...

  6. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  7. Windows7安装CodeTyphon

    WARNING 1: On windows (XP, Vista and Win7), you must install this software as user with administrato ...

  8. ArcGisServer根据最大最小坐标换算瓦片行列号【转】

    1.前言 在上一节中我们知道了屏幕上一像素等于实际中多少单位长度(米或经纬度)的换算方法,而知道这个原理后,接下来我们要怎么用它呢?它和我们前端显示地图有什么关联呢?这一节,我会尽量详细的将这两个问题 ...

  9. cesium js学习一加载三维模型【转】

    http://blog.csdn.net/tangyajun_168/article/details/50936698 最近项目中用到室外三维模型与室内三维地图交互,室外三维模型的加载我们采用了ces ...

  10. fatal error LNK1104: 无法打开文件“libc.lib”的问题 (转)

    今天,编译程序的时候,意外遇到了一个错误,就是VS2008一直提示:fatal error LNK1104: 无法打开文件“libc.lib”,后来在网上查找了很多资料,终于知道原因了... 如果将用 ...