From 9bd0cd1cfccebe6837955f8709350e4f2928693b Mon Sep 17 00:00:00 2001 From: Tinywan <756684177@qq.com> Date: Fri, 14 Apr 2017 11:55:00 +0800 Subject: [PATCH] =?UTF-8?q?Lua=E7=9A=84=E7=9B=B8=E5=AF=B9=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=92=8C=E7=BB=9D=E5=AF=B9=E8=B7=AF=E5=BE=84=E7=9A=84?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E4=BB=A5=E5=8F=8ARedis=E7=9A=84=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E5=B0=81=E8=A3=85=E5=BA=93=E7=9A=84=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 44 ++++++++++++++--- Openresty/lua-common-package/lua-require.md | 53 ++++++++++++++++++++- README.md | 20 ++++---- 3 files changed, 98 insertions(+), 19 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0637c4e..b91b627 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,6 @@ - - + +<<<<<<< Updated upstream +======= + + +>>>>>>> Stashed changes @@ -54,7 +58,7 @@ - + @@ -67,6 +71,7 @@ +<<<<<<< Updated upstream @@ -85,10 +90,13 @@ +======= + +>>>>>>> Stashed changes - + @@ -224,7 +232,8 @@ +<<<<<<< Updated upstream @@ -254,12 +274,17 @@ +<<<<<<< Updated upstream +======= + + +>>>>>>> Stashed changes - + @@ -309,8 +334,13 @@ +<<<<<<< Updated upstream +======= + + +>>>>>>> Stashed changes diff --git a/Openresty/lua-common-package/lua-require.md b/Openresty/lua-common-package/lua-require.md index 174ddfa..dfac031 100644 --- a/Openresty/lua-common-package/lua-require.md +++ b/Openresty/lua-common-package/lua-require.md @@ -1,7 +1,12 @@ ![Markdown](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Images/nginx-hls-locations.png) ### Lua require 相对路径 + [Lua require 相对路径 博客园详解](http://www.cnblogs.com/smallboat/p/5552407.html) -+ 亲自试验总结 ++ 相对路径总结 + + **当前目录** :`/opt/openresty/nginx/conf/Lua` + + 注意:如果在当前没有了,以下的代码运行是没有问题的 + + 就是所有的lua脚本代码全部写在Lua文件夹下面去 + + 拼接路径:`package.path = package.path ..';..\\?.lua';` + + require 直接这样应用就是`Lua.functions` + 目录结构 ``` root@tinywan:/opt/openresty/nginx/conf/Lua# pwd @@ -54,4 +59,48 @@ root@tinywan:/opt/openresty/nginx/conf/Lua# curl '127.0.0.1:80/api' Hello 102 - ``` \ No newline at end of file + ``` ++ 绝对路径总结 + + 目录结果:`/opt/openresty/nginx/lua/resty` + + 包路径:`package.path = package.path ..'/opt/openresty/nginx/lua/resty/?.lua;/opt/openresty/lualib/?.lua';` + + 加载绝对路径的Redis的二次封装库,redis_iresty.lua文件: + ``` + package.path = package.path ..'/opt/openresty/nginx/lua/resty/?.lua;/opt/openresty/lualib/?.lua'; + local redis_c = require "resty.redis" + local ok, new_tab = pcall(require, "table.new") + if not ok or type(new_tab) ~= "function" then + new_tab = function (narr, nrec) return {} end + end + ``` + + Redis的二次封装库的应用,test_redis2.lua文件: + ``` + package.path = '/opt/openresty/nginx/lua/?.lua;'; + local redis = require("resty.redis_iresty") + local red = redis:new() + local ok, err = red:set("Redis1999", "Redis is an animal 1999") + if not ok then + ngx.say("failed to set dog: ", err) + return + end + ``` + + nginx.conf 配置文件(注意以下的目录路径和上面的不一样) + ``` + location /api { + lua_code_cache off; + content_by_lua_file /opt/openresty/nginx/lua/test_redis2.lua; + } + ``` + + CURL 请求结果 + ``` + root@tinywan:/opt/openresty/nginx/conf/Lua# curl '127.0.0.1:80/api' + Hello + 102 + ``` + + Redis 数据库结果 + ``` + 127.0.0.1:6379> keys * + 1) "dog" + 2) "Redis2222222222" + 3) "Redis1999" + ``` + \ No newline at end of file diff --git a/README.md b/README.md index 065fbfa..3cdcddb 100644 --- a/README.md +++ b/README.md @@ -272,18 +272,18 @@ ``` . ├── conf - │ ├── nginx.conf + │ ├── nginx.conf -- Nginx 配置文件 ├── logs - │ ├── error.log + │ ├── error.log -- Nginx 错误日子 │ └── nginx.pid ├── lua - │ ├── access_check.lua - │ ├── addition.lua - │ ├── subtraction.lua - │ ├── multiplication.lua - │ ├── division.lua - │ └── comm - │ └── param.lua + │ ├── access_check.lua -- 权限验证文件 + │ ├── business_redis.lua -- 业务 Redis 处理文件 + │ ├── business_redis.lua -- 业务 Redis 处理文件 + │ ├── ... + │ └── resty -- 存放Lua 的所有公共、封装好的库 + │ └── redis_iresty.lua -- Redis 接口的二次封装 + │ └── param.lua -- 参数过滤库 └── sbin └── nginx ``` @@ -327,7 +327,7 @@ The man name is Tinywan The man name is Phalcon ``` - + [Lua require 相对路径(一个文件引入另外一个文件的Function),已经解决](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-common-package/lua-require.md) + + [Lua require 绝对和相对路径问题(一个文件引入另外一个文件的Function),已经解决](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Openresty/lua-common-package/lua-require.md) + lua-resty-redis 扩展 + 代码引入:`lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;";` + **Lua脚本实现一个CDN的反向代理功能(智能查找CDN节点)(测试成功,可上线)**