Lua中实现签名验证脚本、lua利用ngx实现md5加密、lua-nginx-module模块里ngx_lua的所有指令以及可用ngx所有方法

一、lua中实现签名验证的脚本,lua中利用ngx实现md5加密

    nginx+lua做签名少不了进行md5操作 (publish:August 15, 2017 -Tuesday) ,lua本身要支持md5,需要安装md5支持,github地址:https://github.com/keplerproject/md5

cd /opt/modules/downloads
wget http://luaforge.net/frs/download.php/2746/md5-1.1.1.tar.gz
tar xzvf md5-1.1.1.tar.gz 
cd md5-1.1.1
./configure
Make
make install

    然后把md5.lua,core.so文件以及需要的string.lua文件(在md5.lua中有调用string和core)都放到目录/usr/share/lua/5.1 运行以下lua脚本

package.path = "/usr/share/lua/5.1/?.lua;;"
package.cpath= '/usr/share/lua/5.1/?.so;'
require 'md5'
sign=md5.sumhexa("123456")
print(sign)

    实际应用中lua很多时候都是和nginx一起应用,使用nginx_lua模块中的md5使用更方便(直接用即可)。如下是对我对URL请求里中的签名值进行验证的lua脚本:

--检验请求的sign签名是否正确
--intable:传入的参数值组成的table,不含传入的签名值
--salt:加入到签名字符串中的混杂字符
--insign:请求中传入进来的签名值
function signcheck(intable, salt, insign)
    local keys, tmp = {}, {}
    --提出所有的键名并按字符顺序排序
    for k, _ in pairs(intable) do 
        keys[#keys+1]= k
    end
    sort(keys)
    --根据排序好的键名依次读取值并拼接字符串成key=value&key=value
    for _, k in pairs(keys) do
        if type(intable[k]) == "string" or type(intable[k]) == "number" then 
            tmp[#tmp+1] = k .. "=" .. tostring(intable[k])
        end
    end
    --将salt添加到最后,计算正确的签名sign值并与传入的sign签名对比,
    local signchar = concat(tmp, "&") .. salt
    local rightsign = ngx.md5(signchar);
    if insign ~= rightsign then
        --如果签名错误返回错误信息并记录日志,
        local mess="sign error: insign,"..insign .. " right sign:" ..rightsign.. " sign_char:" .. signchar
        ngx.log(ngx.ERR, mess)
        return false,mess
    end
    return true
end

二、lua-nginx-module模块里ngx_lua的所有指令以及可用ngx所有方法

    在使用lua时,也许我们很多时候都只用了它几个基础的方法:比如 Nginx + Lua 程序脚本开发的一些配置基础及代码示例 以及 使用nginx+lua 做WEB开发的代码实例_nginx lua 脚本开发-CSDN博客 这里面的content_by_lua,或者content_by_lua_file方法。在nginx+lua的脚本中,我们常用的ngx.say,以及ngx.log等,但实际ngx.还有很多好用的方法。

1. lua_nginx_module中可使用的指令列表:

指令名称说明
lua_use_default_type是否使用default_type指令定义的Content-Type默认值
lua_code_cache*_by_lua_file文件是否cache
lua_regex_cache_max_entries
lua_regex_match_limit
lua_package_path用Lua写的lua外部库路径(.lua文件)
lua_package_cpath用C写的lua外部库路径(.so文件)
init_by_luamaster进程启动时挂载的lua代码
init_by_lua_file
init_worker_by_luaworker进程启动时挂载的lua代码,常用来执行一些定时器任务
init_worker_by_lua_file
set_by_lua设置变量
set_by_lua_file
content_by_luahandler模块
content_by_lua_file
rewrite_by_lua
rewrite_by_lua_file
access_by_lua
access_by_lua_file
header_filter_by_luaheader filter模块
header_filter_by_lua_file
body_filter_by_luabody filter模块,ngx.arg[1]代表输入的chunk,ngx.arg[2]代表当前chunk是否为last
body_filter_by_lua_file
log_by_lua
log_by_lua_file
lua_need_request_body是否读请求体,跟ngx.req.read_body()函数作用类似
lua_shared_dict创建全局共享的table(多个worker进程共享)
lua_socket_connect_timeoutTCP/unix 域socket对象connect方法的超时时间
lua_socket_send_timeoutTCP/unix 域socket对象send方法的超时时间
lua_socket_send_lowat设置cosocket send buffer的low water值
lua_socket_read_timeoutTCP/unix 域socket对象receive方法的超时时间
lua_socket_buffer_sizecosocket读buffer大小
lua_socket_pool_sizecosocket连接池大小
lua_socket_keepalive_timeoutcosocket长连接超时时间
lua_socket_log_errors是否打开cosocket错误日志
lua_ssl_ciphers
lua_ssl_crl
lua_ssl_protocols
lua_ssl_trusted_certificate
lua_ssl_verify_depth
lua_http10_buffering
rewrite_by_lua_no_postpone
lua_transform_underscores_in_response_headers
lua_check_client_abort是否监视client提前关闭请求的事件,如果打开监视,会调用ngx.on_abort()注册的回调
lua_max_pending_timers
lua_max_running_timers

2. 在lua程序脚本中可使用的操作方法及可取得的变量和常量列表

操作指令说明
ngx.arg指令参数,如跟在content_by_lua_file后面的参数
ngx.var变量,ngx.var.VARIABLE引用某个变量
ngx.ctx请求的lua上下文
ngx.header响应头,ngx.header.HEADER引用某个头
ngx.status响应码
API说明
ngx.log输出到error.log
print等价于 ngx.log(ngx.NOTICE, ...)
ngx.send_headers发送响应头
ngx.headers_sent响应头是否已发送
ngx.resp.get_headers获取响应头
ngx.timer.at注册定时器事件
ngx.is_subrequest当前请求是否是子请求
ngx.location.capture发布一个子请求
ngx.location.capture_multi发布多个子请求
ngx.exec
ngx.redirect
ngx.print输出响应
ngx.say输出响应,自动添加'\n'
ngx.flush刷新响应
ngx.exit结束请求
ngx.eof
ngx.sleep无阻塞的休眠(使用定时器实现)
ngx.get_phase
ngx.on_abort注册client断开请求时的回调函数
ndk.set_var.DIRECTIVE
ngx.req.start_time请求的开始时间
ngx.req.http_version请求的HTTP版本号
ngx.req.raw_header请求头(包括请求行)
ngx.req.get_method请求方法
ngx.req.set_method请求方法重载
ngx.req.set_uri请求URL重写
ngx.req.set_uri_args
ngx.req.get_uri_args获取请求参数
ngx.req.get_post_args获取请求表单
ngx.req.get_headers获取请求头
ngx.req.set_header
ngx.req.clear_header
ngx.req.read_body读取请求体
ngx.req.discard_body扔掉请求体
ngx.req.get_body_data
ngx.req.get_body_file
ngx.req.set_body_data
ngx.req.set_body_file
ngx.req.init_body
ngx.req.append_body
ngx.req.finish_body
ngx.req.socket
ngx.escape_uri字符串的url编码
ngx.unescape_uri字符串url解码
ngx.encode_args将table编码为一个参数字符串
ngx.decode_args将参数字符串编码为一个table
ngx.encode_base64字符串的base64编码
ngx.decode_base64字符串的base64解码
ngx.crc32_short字符串的crs32_short哈希
ngx.crc32_long字符串的crs32_long哈希
ngx.hmac_sha1字符串的hmac_sha1哈希
ngx.md5返回16进制MD5
ngx.md5_bin返回2进制MD5
ngx.sha1_bin返回2进制sha1哈希值
ngx.quote_sql_strSQL语句转义
ngx.today返回当前日期
ngx.time返回UNIX时间戳
ngx.now返回当前时间
ngx.update_time刷新时间后再返回
ngx.localtime
ngx.utctime
ngx.cookie_time返回的时间可用于cookie值
ngx.http_time返回的时间可用于HTTP头
ngx.parse_http_time解析HTTP头的时间
ngx.re.match
ngx.re.find
ngx.re.gmatch
ngx.re.sub
ngx.re.gsub
ngx.shared.DICT
ngx.shared.DICT.get
ngx.shared.DICT.get_stale
ngx.shared.DICT.set
ngx.shared.DICT.safe_set
ngx.shared.DICT.add
ngx.shared.DICT.safe_add
ngx.shared.DICT.replace
ngx.shared.DICT.delete
ngx.shared.DICT.incr
ngx.shared.DICT.flush_all
ngx.shared.DICT.flush_expired
ngx.shared.DICT.get_keys
ngx.socket.udp
udpsock:setpeername
udpsock:send
udpsock:receive
udpsock:close
udpsock:settimeout
ngx.socket.tcp
tcpsock:connect
tcpsock:sslhandshake
tcpsock:send
tcpsock:receive
tcpsock:receiveuntil
tcpsock:close
tcpsock:settimeout
tcpsock:setoption
tcpsock:setkeepalive
tcpsock:getreusedtimes
ngx.socket.connect
ngx.thread.spawn
ngx.thread.wait
ngx.thread.kill
coroutine.create
coroutine.resume
coroutine.yield
coroutine.wrap
coroutine.running
coroutine.status
ngx.config.debug编译时是否有 --with-debug选项
ngx.config.prefix编译时的 --prefix选项
ngx.config.nginx_version返回nginx版本号
ngx.config.nginx_configure返回编译时 ./configure的命令行选项
ngx.config.ngx_lua_version返回ngx_lua模块版本号
ngx.worker.exiting当前worker进程是否正在关闭(如reload、shutdown期间)
ngx.worker.pid返回当前worker进程的pid
常量说明
Core constantsngx.OK (0)
ngx.ERROR (-1)
ngx.AGAIN (-2)
ngx.DONE (-4)
ngx.DECLINED (-5)
ngx.nil
HTTP method constantsngx.HTTP_GET
ngx.HTTP_HEAD
ngx.HTTP_PUT
ngx.HTTP_POST
ngx.HTTP_DELETE
ngx.HTTP_OPTIONS  
ngx.HTTP_MKCOL    
ngx.HTTP_COPY      
ngx.HTTP_MOVE     
ngx.HTTP_PROPFIND 
ngx.HTTP_PROPPATCH 
ngx.HTTP_LOCK 
ngx.HTTP_UNLOCK    
ngx.HTTP_PATCH   
ngx.HTTP_TRACE  
HTTP status constantsngx.HTTP_OK (200)
ngx.HTTP_CREATED (201)
ngx.HTTP_SPECIAL_RESPONSE (300)
ngx.HTTP_MOVED_PERMANENTLY (301)
ngx.HTTP_MOVED_TEMPORARILY (302)
ngx.HTTP_SEE_OTHER (303)
ngx.HTTP_NOT_MODIFIED (304)
ngx.HTTP_BAD_REQUEST (400)
ngx.HTTP_UNAUTHORIZED (401)
ngx.HTTP_FORBIDDEN (403)
ngx.HTTP_NOT_FOUND (404)
ngx.HTTP_NOT_ALLOWED (405)
ngx.HTTP_GONE (410)
ngx.HTTP_INTERNAL_SERVER_ERROR (500)
ngx.HTTP_METHOD_NOT_IMPLEMENTED (501)
ngx.HTTP_SERVICE_UNAVAILABLE (503)
ngx.HTTP_GATEWAY_TIMEOUT (504) 
Nginx log level constantsngx.STDERR
ngx.EMERG
ngx.ALERT
ngx.CRIT
ngx.ERR
ngx.WARN
ngx.NOTICE
ngx.INFO
ngx.DEBUG
 
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林戈的IT生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值