1 Star 0 Fork 270

Andy / lua-nginx-redis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
openresty-resty-module.md 12.77 KB
一键复制 编辑 原始数据 按行查看 历史
Tinywan 提交于 2017-09-15 15:28 . OR

lua-resty-redis 扩展 (是openresty下操作redis的模块)

  • 代码引入:lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;";

  • Lua脚本实现一个CDN的反向代理功能(智能查找CDN节点)(测试成功,可上线)

    • nginx.conf 配置信息
    http {
        lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;";
        server {
            listen 80;
            server_name  localhost;
            location ~ \/.+\/.+\.(m3u8|ts) {
                if ($uri ~ \/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)(|-).*\.(m3u8|ts)) {
                        set $app_name $1;
                        set $a $2;
                }
                set $stream_id "";
                default_type 'text/html';
                rewrite_by_lua_file  /opt/openresty/nginx/lua/proxy_pass_cdn.lua;
                proxy_connect_timeout       10;
                proxy_send_timeout          30;
                proxy_read_timeout          30;
                proxy_pass                  $stream_id;
            }
    
        }
    }
  • Lua脚本结合 Nginx+Lua+Local Redis+Mysql服务器缓存

    • Nginx+Lua+Local Redis+Mysql集群架构

    • Nginx+Lua+Local Redis+Mysql

    • Lua脚本Nginx+Lua+Redis+Mysql.lua

    • Nginx.conf配置文件Nginx+Lua+Redis+Mysql.conf

    • HELP

    • Lua脚本结合 Redis 统计直播流播放次数、链接次数等等信息

      • nginx.conf
        server {            # 配置虚拟服务器80
             listen 80;
             server_name  127.0.0.1:8088;
             location ~* /live/(\w+)/ {
                    set $total_numbers "";
                    set $stream_name $1;
                    lua_code_cache off;
                    rewrite_by_lua_file /opt/openresty/nginx/conf/Lua/total_numbers.lua;
                    proxy_pass                  http://127.0.0.1:8088;
              }
        }      
      • 代理服务器
         server {            # 配置虚拟服务器8088
             listen 8088;
             server_name  127.0.0.1:8088;
             location /live {
                add_header  Cache-Control no-cache;
                add_header 'Access-Control-Allow-Origin' '*' always;
                add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
                add_header 'Access-Control-Allow-Headers' 'Range';
                types{
                    application/dash+xml mpd;
                    application/vnd.apple.mpegurl m3u8;
                    video/mp2t ts;
                }
             alias /home/tinywan/HLS/live/;
            }
          }
        
      • CURL请求地址:http://192.168.18.143/live/tinywan123/index.m3u8
      • Lua 脚本

lua-resty-websocket 扩展

  • 代码引入:lua_package_path "/opt/openresty/nginx/lua/lua-resty-websocket/lib/?.lua;;";
  • Lua脚本实现一个websocket连接(测试成功,可上线)
    • nginx.conf 配置信息
    http {
            lua_package_path "/opt/openresty/nginx/lua/lua-resty-websocket/lib/?.lua;;";
            server {
                listen 80 so_keepalive=2s:2s:8;  #为了防止半开TCP连接,最好在Nginx监听配置指令中启用TCP keepalive
                server_name  localhost;
                location /ws {
                    lua_socket_log_errors off;
                    lua_check_client_abort on;
                    lua_code_cache off; # 建议测试的时候最好关闭缓存
                    content_by_lua_file /opt/openresty/nginx/conf/Lua/websocket.lua;
                }
            }
    }

lua-cjson 扩展

  • 基本用法
    • nginx.conf
      location /cjson {
          content_by_lua_block {
              local cjson = require "cjson"
              local json = cjson.encode({
                      foo = "bar",
                      some_object = {},
                      some_array = cjson.empty_array
              })
              ngx.say(json)
          }
      }
    • curl 请求
      root@tinywan:/opt/openresty/nginx/conf# curl http://127.0.0.1/cjson
      {"some_object":{"tel":13669313112,"age":24},"name":"tinywan","some_array":[]}
  • lua对象到字符串、字符串到lua对象

lua-resty-session 扩展

  • OpenResty 引用第三方 resty 库非常简单,只需要将相应的文件拷贝到 resty 目录下即可
  • 我服务器OpenResty 的 resty 路径:/opt/openresty/lualib/resty
  • 下载第三方 resty 库:git clone lua-resty-session 文件路径以及内容:
    tinywan@tinywan:/opt/openresty/nginx/lua/lua-resty-session/lib/resty$ ls
    session  session.lua
  • 特别注意:这里拷贝的时候要要把session文件和session.lua 文件同时吧、拷贝过去,否则会报错误:
    /opt/openresty/lualib/resty/session.lua:34: in function 'prequire'
    /opt/openresty/lualib/resty/session.lua:211: in function 'new'
    /opt/openresty/lualib/resty/session.lua:257: in function 'open'
    /opt/openresty/lualib/resty/session.lua:320: in function 'start'
  • 拷贝完毕后/opt/openresty/lualib/resty OpenResty 引用第三方 resty 的所有库文件
tinywan@tinywan:/opt/openresty/lualib/resty$ ls
aes.lua  core.lua  http_headers.lua  lock.lua  lrucache.lua  memcached.lua  random.lua  session      sha1.lua    sha256.lua  sha512.lua  string.lua  upstream
core     dns       http.lua          lrucache  md5.lua       mysql.lua      redis.lua   session.lua  sha224.lua  sha384.lua  sha.lua     upload.lua  websocket
  • 基本用法
     location /start {
        content_by_lua_block {
            local session = require "resty.session".start()
            session.data.name = "OpenResty Fan Tinywan"
            session:save()
            ngx.say("<html><body>Session started. ",
                    "<a href=/test>Check if it is working</a>!</body></html>")
            ngx.say(session.data.name,"Anonymous")
        }
     }
  • curl 请求
    tinywan@tinywan:/opt/openresty/nginx/conf$ curl http://192.168.18.143/start
    <html><body>Session started. <a href=/test>Check if it is working</a>!</body></html>
    OpenResty Fan Tinywan Anonymous

Lua 权限验证

  • Lua 一个HLS的简单地址访问权限验证
    • Nginx.conf 配置
      location ^~ /live/ {
          add_header Cache-Control no-cache;
          add_header 'Access-Control-Allow-Origin' '*' always;
          add_header 'Access-Control-Allow-Credentials' 'true';
          add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
          add_header 'Access-Control-Allow-Headers' 'Range';
      
          types{
              application/dash+xml mpd;
              application/vnd.apple.mpegurl m3u8;
              video/mp2t ts;
          }
          if ( $uri ~ \.m3u8 ) {
              lua_code_cache off;
              access_by_lua_file /opt/openresty/nginx/lua/access.lua;
          }
          root /home/tinywan/HLS;
       }
    • access.lua 文件内容
      if ngx.req.get_uri_args()["wsSecret"] ~= "e65e6a01cf26523e206d5bb0e2a8a95a" then  
         return ngx.exit(403)  
      end

lua-resty-string 扩展

  • MD5加密的简单基本用法 md5.lua
    local resty_md5 = require "resty.md5"
    local md5 = resty_md5:new()
    if not md5 then
        ngx.say("failed to create md5 object")
        return
    end
    local ok = md5:update("hello")
    if not ok then
        ngx.say("failed to add data")
        return
    end
    local digest = md5:final()
    -- ngx.say("md5",digest)                ---注意:这样直接输出是乱码
    local str = require "resty.string"
    ngx.say("md5: ", str.to_hex(digest))    ---注意:必须通过字符串转码方可打印输出
        -- yield "md5: 5d41402abc4b2a76b9719d911017c592"

lua-resty-http 扩展 (ngx_lua的HTTP客户端cosocket驱动程序)

lua-resty-mysql 扩展

srcache-nginx-module 扩展 (nginx下的一个缓存模块)

openresty扫描代码全局变量

  • 在OpenResty中需要避免全局变量的使用,为此春哥写了一个perl工具,可以扫描openresty lua代码的全局变量
  • https://github.com/openresty/openresty-devel-utils/blob/master/lua-releng
  • 用法相当简单
    1. 将代码保存成lua-releng文件
    2. 更改lua-releng的权限,chmod 777 lua-releng
    3. 假设有一个源码文件为test.lua
    4. 执行./lua-releng test.lua,则会扫描test.lua文件的全局变量,并在屏幕打印结果
Lua
1
https://gitee.com/andy_w/lua-nginx-redis.git
git@gitee.com:andy_w/lua-nginx-redis.git
andy_w
lua-nginx-redis
lua-nginx-redis
master

搜索帮助