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#编程(十二)----------函数
类和结构 类和结构实际上都是创建对象的模板 ,每 个对象都包含数据 ,并 提供了处理和访问数据的方法. 类定义了类的每个对象 (称 为实例 )可 以包含什么数据和功能 . 例如 ,如 果 一 个类表示 ...
- The Topo to Raster tool returns errors 010235 and 010067转
Problem: The Topo to Raster tool returns errors 010235 and 010067 Description The Topo to Raster geo ...
- 分布式系统:CAP
一致不太理解CAP,最近好像有点感觉了,这里写下来,先介绍下CAP的定义: C:一致性.写完数据后,立马能看到最新数据. A:可用性.所有请求必须有响应. P:分区容错性.网络或服务器故障不会导致系统 ...
- 波吉亚家族第一季/全集The Borgias 1迅雷下载
波吉亚家族 第一季 The Borgias Season 1 (2011)本季看点:<波吉亚家族>是一个非常复杂的故事,是现代人描绘这个臭名昭著的王朝家族过往历史的一副有趣又坦率的肖像画. ...
- MongoDB索引,性能分析
索引的限制: 索引名称不能超过128个字符 每个集合不能超过64个索引 复合索引不能超过31列 MongoDB 索引语法 db.collection.createIndex({ <field&g ...
- TextView 中文文档
属性名称 描述 android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/all) ...
- Java遍历对象所有属性
要获取对象的所有属性可以使用getDeclaredFields()方法会返回一个Field数组遍历这个数组几个遍历所有属性注意使用这个方法会抛出4个异常然后根据属性的类型选择执行对应的内容 publi ...
- 洛谷 P1164 小A点菜
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过uim由于买了一些辅(e ...
- C#多线程写日志
由于程序是3层架构的,所有多线程记录日志成了比较棘手的问题,以前还真就没有在意过写日志的问题,认为不过是写文件罢了~~!如今发现原来要实现文件共享,并且能够使多线程同时操作日志还不能相互冲突,真的很麻 ...
- hdu-4466-Triangle 数学题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4466 题目意思: 一根长为N的木棒,长度分成若干个三角形,使得任意两个三角形都相似.对应顺序三角形全 ...