
nginx/lua/openresty
xiangjie256
这个作者很懒,什么都没留下…
展开
-
lua基本使用
注释:单行注释:--多行注释:--[[--]][code="lua"]function norm (x, y) local n2 = x^2 + y^2 return math.sqrt(n2)endfunction twice (x) return 2*xendprint(norm(1,2))print(twice(3))...原创 2015-03-19 14:47:22 · 119 阅读 · 0 评论 -
lua_shared_dict
1.定义一块名为my_cache的共享内存空间,内存大小为size。2.通过该命令定义的共享内存对象对于Nginx中所有worker进程都是可见的3.当Nginx通过reload命令重启时,共享内存字典项会从新获取它的内容4.当时当Nginx退出时,字典项的值将会丢失[code="nginx.conf"]http{ lua_shared_dict my_cache 128...原创 2017-05-18 12:01:19 · 3557 阅读 · 0 评论 -
openresty缓存
shared dict:这个cache是nginx所有worker之间共享的,内部使用的LRU算法(最近最少使用)来判断缓存是否在内存占满时被清除。[code="lua"]function get_from_cache(key) local cache_ngx = ngx.shared.my_cache local value = cache_ngx:get(key...原创 2016-03-09 15:12:28 · 308 阅读 · 0 评论 -
cjson
[code="lua"]local json = require("cjson")local data = {1, 2}data[1] = 99-- ... do the other thingsngx.say(json.encode(data))ngx.say("value --> ", json.encode({dogs={}}))local s...原创 2016-03-07 10:42:33 · 90 阅读 · 0 评论 -
nginx使用lua文件
[code="ngx conf"]location /test_var { content_by_lua_file lua/test_var.lua;}[/code]test_var.lua[code="lua"]ngx.say(ngx.var.remote_addr)[/code]取内部返回值[code="ngx conf"]loca...原创 2016-03-02 10:13:22 · 826 阅读 · 0 评论 -
获取 uri 参数
[code="lua"] location /test { content_by_lua_block { local arg = ngx.req.get_uri_args() for k,v in pairs(arg) do ngx.say("[GET] key:", k, " v:", v)...原创 2016-02-23 11:38:04 · 586 阅读 · 0 评论 -
反向代理
反向代理(Reverse Proxy )方式是指用代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。举个例子,一个用户访问http://www.example.com/readme,但是www.example.com上并不存在readme页面,它是偷偷...原创 2016-02-22 11:25:56 · 89 阅读 · 0 评论 -
location 匹配规则
转自[url]https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx/nginx_local_pcre.html[/url]location 匹配规则语法规则location [=|~|~*|^~] /uri/ { … }location多个location配置的情况下匹配顺序为...原创 2016-02-22 10:25:19 · 234 阅读 · 0 评论 -
lua点与冒号的区别
冒号操作会带入一个self参数,用来代表自己。而点号操作,只是内容的展开。在函数定义时,使用冒号将默认接收一个self参数,而使用点号则需要显式传入self参数。[code="lua"]local A={x=20}function A:fun1() print(self.x)endlocal B={x=20}function B.fun1(self)...原创 2016-01-29 11:04:31 · 227 阅读 · 0 评论 -
openresty文件自适应
[code="nginx.conf"]location ~ ^/([-_a-zA-Z0-9/]+) { content_by_lua_file src/$1.lua;} [/code]原创 2017-05-18 14:14:06 · 152 阅读 · 0 评论 -
ngx timer
[code="lua"]local delay=3local handlehandle = function(param1,param2) if param1 then return end ngx.log(ngx.ERR,"param is " ..param2) ngx.timer.at(delay,handle,"he...原创 2017-05-20 08:11:44 · 334 阅读 · 0 评论 -
lua weak表与gc
[code="lua"]local a = {}local b = {}setmetatable(a, b)--这个 table 中的 vaules 就是 weakb.__mode = "k" -- now 'a' has weak keyslocal t1 = {}local t2 = {} -- creates first key a[t1] = 0a[t2...原创 2017-11-29 09:56:51 · 163 阅读 · 0 评论 -
lua single-method
[code="lua"]local function newObject (value) return function (action, v) if action == "get" then return value elseif action == "set" then value = v else error("invalid action") ...原创 2017-11-29 09:49:22 · 134 阅读 · 0 评论 -
lua ffi
testlib.c[code="c"]#include #include char* fun_strcat(char* str1,char* str2){ return strcat(str1,str2);}[/code]生成动态库:gcc -g -o testLib.so -fpic -shared testlib.c[code="l...原创 2017-11-16 11:33:39 · 349 阅读 · 0 评论 -
lua unpack
unpack接受一个数组作为输入参数,返回数组的所有元素。被用来实现范型调用机制[code="lua"]local lst = {'a','b','c','d',{1,2,3}}print(unpack(lst))a b c d table: 0x64f5f890[/code]...原创 2017-11-09 11:07:55 · 169 阅读 · 0 评论 -
resty thread
[code="lua"]local t = {}local function produce_thread() for i=1,5 do t[i] = i ngx.say("produce:",i) ngx.sleep(0.01) endendlocal function consume_thread() for i=1,5 ...原创 2017-07-07 14:35:50 · 159 阅读 · 0 评论 -
resty 正则
[code="lua"]local m, err = ngx.re.match("hello, 1234", "[0-9]+")if m then ngx.say("match,m[0]:",m[0]) ngx.say("match,m[1]:",m[1])else if err then ngx.log(ngx.ERR, "error: ", er原创 2017-07-04 17:32:15 · 152 阅读 · 0 评论 -
Nginx Lua脚本执行顺序
[img][img]http://dl2.iteye.com/upload/attachment/0125/4510/df4868cd-a650-3f4a-b97d-814a09c00d59.png[/img][/img]原创 2017-06-15 09:11:30 · 259 阅读 · 0 评论 -
openresty资料
视频[url]http://study.163.com/course/introduction.htm?courseId=1520005#/courseDetail[/url][url]http://www.stuq.org/course/detail/1015[/url]书[url]https://moonbingbing.gitbooks.io/openresty-be...原创 2016-03-15 09:57:13 · 101 阅读 · 0 评论 -
虚变量
[code="lua"]local _,a = 1,2 --虚变量(即下划线),接收1,然后丢弃print(a)print("virtual in for:")local t = {1, 3, 5}for _,v in ipairs(t) do print(v) end 2virtual in...原创 2016-01-29 10:42:55 · 215 阅读 · 0 评论 -
table判空
[code="lua"]function isTableEmpty(t) if t == nil or _G.next(t) == nil then return true else return false endend[/code]原创 2016-01-28 15:18:14 · 143 阅读 · 0 评论 -
lua book
[url]http://lua.ren/topic/157/lua书[/url]原创 2016-05-05 10:26:20 · 93 阅读 · 0 评论 -
lrucache
[code="lua"]local _M = {}--local lrucache = require "resty.lrucache.pureffi"local lrucache = require "resty.lrucache"-- we need to initialize the cache on the lua module level so that-- ...原创 2016-04-19 16:21:44 · 228 阅读 · 0 评论 -
lua函数
传引用[code="lua"]local function test(a) a.a1 = 3endlocal a = {a1=1,a2=2}test(a)print(a.a1)3[/code]非全局函数[code="lua"]Lib = {}function Lib.foo (x,y) return x + yendf...原创 2015-03-20 15:52:54 · 102 阅读 · 0 评论 -
lua控制语句
[code="lua"]a,b,c=0,1,2print(a,b,c)print("local:")x = 10a = 3if a原创 2015-03-20 11:01:47 · 129 阅读 · 0 评论 -
lua表
不要在 lua 的 table 中使用 nil 值,如果一个元素要删除,直接 remove,不要用 nil 去代替。[code="lua"]local table1 = {a=1,"2",c="3","4","5"}print("size:"..#table1)print("size:"..table.getn(table1))print(table原创 2015-03-20 10:24:51 · 106 阅读 · 0 评论 -
lua操作符
连接符[code="lua"]print(1 .. 2)--number need add spaceprint("a".."b")print("abc".."123")12ababc123[/code][code="lua"]print("1"原创 2015-03-20 09:44:32 · 145 阅读 · 0 评论 -
lua字符串
[code="lua"]local s = "hello abc"print(string.sub(s,1,3))print(string.find(s,"o"))--start and end index(from 1)local gs = string.gsub("abc","a","b")--replace a to bprint(gs)hel原创 2015-03-20 09:41:40 · 99 阅读 · 0 评论 -
lua值与类型
Lua 中有八种基本类型: nil, boolean, number, string, function, userdata, thread, and table. Nil 类型只有一种值 nil ,它的主要用途用于标表识和别的任何值的差异Boolean 类型只有两种值:false 和 true。 nil 和 false 都能导致条件为假userdata 类型用来将任意 C 数...原创 2015-03-19 15:02:20 · 124 阅读 · 0 评论 -
lua loadstring
loadstring 不会有边界效应产生,他仅仅编译 chunk 成为自己内部实现的一个匿名函数。通常对他的误解是他们定义了函数。Lua 中的函数定义是发生在运行时的赋值而不是发生在编译时loadstring 函数功能强大,但使用时需多加小心.确认没有其它简单的解决问题的方法再使用[code="lua"]f = loadstring("i = i * 3")i = 1f...原创 2016-11-29 10:08:54 · 310 阅读 · 0 评论 -
lua 协程(coroutine)
[code="lua"]local function f(a) local thread = coroutine.running() ngx.say("status:",coroutine.status(thread)) ngx.say("1") coroutine.yield() ngx.say("2") coroutine.yield()end...原创 2016-11-30 10:34:51 · 141 阅读 · 0 评论 -
lua面向对象
[code="lua"]local A = {}local mt = { __index = A }function A.add (self, v) self.value = self.value + vendfunction A.new (self, value) value = value or 0 return setmet...原创 2016-01-28 12:16:43 · 89 阅读 · 0 评论 -
lua元表
元表 (metatable) 的表现行为类似于C++语言中的操作符重载setmetatable(table,metatable):此方法用于为一个表设置元表。getmetatable(table):此方法用于获取表的元表对象。[code="lua"]local function add_fun(self,another) local result = {} loca...原创 2016-01-28 10:06:49 · 93 阅读 · 0 评论 -
lua学习
[url]https://moonbingbing.gitbooks.io/openresty-best-practices/content/[/url]原创 2016-01-27 15:30:58 · 93 阅读 · 0 评论 -
lua文件读写
"r" 读模式 (默认) 返回nil加错误信息"w" 写模式 创建文件"a" 添加模式 创建文件"r+" 更新模式,保存之前的数据 返回nil加错误信息"w+" 更新模式,清除之前的数据 创建文件"a+" 添加更新模式,保存之前的数据,在文件尾进行添加 创建文件模式字符串后面可以有一个'b',用于在某些系统中打开二进制文件。注意 "w"原创 2016-01-27 15:19:56 · 296 阅读 · 0 评论 -
lua全动态函数调用
[code="lua"]local function run(x, y) ngx.say('run', x, y)endlocal function attack(targetId) ngx.say('targetId', targetId)endlocal function doAction(method, ...) local args...原创 2016-01-26 14:26:08 · 1270 阅读 · 0 评论 -
lua循环
for[code="lua"]for i=1,5 do print(i)end12345[/code][code="lua"]local a = {"a", "b", "c", "d"}for k, v in ipairs(a) do print("index:", k, " value:原创 2016-01-22 11:47:22 · 79 阅读 · 0 评论 -
lua数组
数组下标是从1开始:[code="lua"]local arr1 = {1, 2, 3,6, [5]=5}print(arr1[4])print(arr1[5])65[/code]原创 2016-01-18 10:46:46 · 88 阅读 · 0 评论 -
nginx/lua学习
[url]http://jinnianshilongnian.iteye.com/category/333854[/url][url]https://moonbingbing.gitbooks.io/openresty-best-practices/content/[/url]原创 2016-01-18 10:12:01 · 90 阅读 · 0 评论 -
nginx与lvs的异同
[url]http://blog.youkuaiyun.com/BuquTianya/article/details/52076153[/url][url]http://blog.51cto.com/4593973/1419072[/url]nginx用来做http的反向代理,能够upsteam实现http请求的多种方式的均衡转发。由于采用的是异步转发可以做到如果一个服务器请求失败,立即切换到其他...原创 2017-11-30 11:51:06 · 368 阅读 · 0 评论