最近经常使用春哥和小哲老师写的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. Linux netstat命令具体解释

    简单介绍 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表.接口状态 (Interface Statistics).masquerade 连接,多播成员 (Multicast Memb ...

  2. Spring+Velocity(平台升级至Spring Framework 5.0.2)

    下载: http://repo.spring.io/release/org/springframework/spring/ Dear Spring community, I’m pleased to ...

  3. C++MFC编程笔记day05 文档类-单文档和多文档应用程序

    文档类 1 相关类    CDocument类-父类是CCmdTarget类,所以,文档类也能够处理菜单等               命令消息. 作用保存和管理数据.    注意事项:怎样解决断言错 ...

  4. Binary Search Tree 以及一道 LeetCode 题目

    一道LeetCode题目 今天刷一道LeetCode的题目,要求是这样的: Given a binary search tree and the lowest and highest boundari ...

  5. Leetcode刷题记录:编码并解码短网址

    题目要求 编写一个类,提供两个方法.一个可以将普通的网址编码成短网址,一个可以将短网址还原为普通网址. 参考题解 # 使用随机函数,生成短网址,保存在dict中,避免重复 import random ...

  6. sys.stdout.flush()以及subprocess的用处

    sys.stdout.flush()立即把stdout缓存内容输出. subprocess与shell进行交互,执行shell命令等. 执行shell命令集合: subprocess.check_ou ...

  7. storm的一些相关文章

    文章可以看这些: https://www.cnblogs.com/zhaojiankai/p/7257617.html https://blog.csdn.net/wangshuminjava/art ...

  8. 如何查看Isilon节点的硬件信息?

    Isilon节点虽然是一个Linux,但是很多linux下常用的命令都没有,比如说看内存的.笔者经过试验,列出了一些可用的命令. 查看硬件状态 isi_hw_status 查看内存 sysctl hw ...

  9. [PowerShell Utils] Automatically Change DNS and then Join Domain

    I would like to start a series of blog posts sharing PowerShell scripts to speed up our solution ope ...

  10. IO流 简介 总结 API 案例 MD

    目录 IO 流 简介 关闭流的正确方式 关闭流的封装方法 InputStream 转 String 的方式 转换流 InputStreamReader OutputStreamWriter 测试代码 ...