OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。
Nginx 是俄罗斯人发明的, Lua 是巴西几个教授发明的,中国人章亦春把 LuaJIT VM 嵌入到 Nginx 中,实现了 OpenResty 这个高性能服务端解决方案。
Openresty其实只是个软件包,它基于nginx+lua模块,集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。
如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。
http://openresty.org/cn/download.html
这是openresty和nginx的目录结构对比(windows和linux略有不同):
下面是一个登录的lua脚本,在openresty启动时登录指定的地址。这个脚本定义了一个login方法,并通过return _M将方法暴露出去。
local _M = { _VERSION = '0.1' }
function _M.login(userName, password)
local s = io.popen('ipconfig | findstr \"IPv4\"')
local m = s:read("*all")
local ip = ''
for w in m:gmatch (": (%d+%.%d+%.%d+%.%d+)") do
ip = ip..w..','
end
if ip ~= '' then
ip = string.sub(ip, 1, -2)
end
local http = require "resty.http"
local httpc = http.new()
local url = "https://www.xxx.com/login"
local resty_md5 = require "resty.md5"
local md5 = resty_md5:new()
md5:update(password.."salt")
local digest = md5:final()
local str = require "resty.string"
str = str.to_hex(digest)
local bodyparam = "userName="..userName.."&password="..str
local res, err = httpc:request_uri(url, {
method = "POST",
ssl_verify = false,
body = bodyparam,
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["source"] = "boxapp"
}
})
local json = require("cjson")
resStr = json.decode(res.body)
code = resStr.errorCode
if code == "00000"
then
ngx.log(ngx.ERR,"login success:code:"..resStr.errorCode)
if resStr.data == "" then
ngx.log(ngx.ERR,userName.." has not apply the token")
return
end
return
else
ngx.log(ngx.ERR,"login failed:code:"..resStr.errorCode..",msg:"..resStr.message)
end
end
return _M
上一步定义了login方法,接下来就可以调用了。下面是init.lua文件。 local handler = function (premature) if premature then return end local login = require "login" local config = require "config" login.login(config.getUserName(), config.getPassword()) ngx.ctx.dir = fileDir end
local ok, err = ngx.timer.at(3, handler)
然后可以在启动openresty的时候,去登录并注册信息。
init_worker_by_lua_file "./lualib/init.lua";
server {
listen 8808;
server_name localhost;
client_max_body_size 10m;
.......
当openresty登录成功了,我们可能想使用openresty对访问的路径进行鉴权。
鉴权lua脚本checkLogin.lua:
--getimage.lua文件内容
--从header中获取token值
local args = ngx.req.get_uri_args()
local token = args["token"]
--判断token是否为空,为空则直接400
if not token
then
ngx.exit(400)
end
local http = require "resty.http"
local httpc = http.new()
local url = "https://www.XXXXX.com/check"
local resStr="false" --响应结果
local res, err = httpc:request_uri(url, {
method = "POST",
ssl_verify = false,
body = "{\"token\":\""..token.."\", \"type\":5}",
headers = {
["Content-Type"] = "application/json",
}
})
--判断接口返回结果状态
if not res then
ngx.log(ngx.ERR,"failed to request: ", err)
ngx.exit(401)
end
--请求之后,状态码
ngx.status = res.status
if ngx.status ~= 200 then
ngx.log(ngx.ERR,"非200状态,ngx.status:"..ngx.status)
ngx.exit(402)
end
--响应的内容 如果结果用户id存在,则重定向图片服务器
resStr = res.body
local json = require("cjson")
code = json.decode(resStr).errorCode
if code == "00000"
then
-- ngx.log(ngx.ERR,"failed to request: ")
return
else
ngx.exit(403)
end
然后在nginx这样配置:
location /list.json {
lua_code_cache off; #热部署,每次修改lua文件,不用重新加载部署
content_by_lua_file ./lualib/checkLogin.lua;
}
这样,一套登录和鉴权的web系统就做好了,关键是性能好。
使用lua-rest在github上搜索,会看到很多openresty的扩展,在apisix中,使用了很多扩展组件。使用 luarocks(Luarocks 是一个 Lua 包管理器,基于 Lua 语言开发,提供一个命令行的方式来管理 Lua 包依赖、安装第三方 Lua 包等)进行安装。
所以,crud可以不用装jvm了(https://github.com/openresty/lua-resty-mysql)。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有