前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Openresty能帮我们做什么

Openresty能帮我们做什么

作者头像
品茗IT
发布2023-10-22 15:14:46
2420
发布2023-10-22 15:14:46
举报
文章被收录于专栏:品茗IT品茗IT

Openresty能帮我们做什么

一、Openresty是什么

OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。

Nginx 是俄罗斯人发明的, Lua 是巴西几个教授发明的,中国人章亦春把 LuaJIT VM 嵌入到 Nginx 中,实现了 OpenResty 这个高性能服务端解决方案。

Openresty其实只是个软件包,它基于nginx+lua模块,集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。

如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。

二、Openresty怎么用

http://openresty.org/cn/download.html

  1. 去官网下载,找到对应版本,下载并解压
  2. windows上进入目录直接执行./nginx.exe,重启使用nginx -s reload。和普通nginx使用方法一样。

这是openresty和nginx的目录结构对比(windows和linux略有不同):

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
  • xxx_temp是都是nginx一些模块开启后缓存文件的目录,openresty把这些模块都整合了。
  • lua是luajit的目录,也就是运行时编译器,windows的有exe文件,目录里存放的是一些jit的lua文件。
  • lualib是OpenResty 中使用到的 Lua 库,主要分为 ngx 和 resty 两个子目录。
  • pod 是 Perl 里面的一种标记语言,用于给 Perl 的模块编写文档。pod 目录中存放的就是OpenResty、 NGINX、lua-resty-*、LuaJIT 的文档。

三、Openresty怎么写

3.1 登录示例

下面是一个登录的lua脚本,在openresty启动时登录指定的地址。这个脚本定义了一个login方法,并通过return _M将方法暴露出去。

代码语言:javascript
复制
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的时候,去登录并注册信息。

代码语言:javascript
复制
init_worker_by_lua_file "./lualib/init.lua";
        
server {
    listen 8808;
    server_name  localhost;
    client_max_body_size 10m;
.......
3.2 鉴权示例

当openresty登录成功了,我们可能想使用openresty对访问的路径进行鉴权。

鉴权lua脚本checkLogin.lua:

代码语言:javascript
复制
--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这样配置:

代码语言:javascript
复制
location /list.json  {
    lua_code_cache off; #热部署,每次修改lua文件,不用重新加载部署
    content_by_lua_file ./lualib/checkLogin.lua;
}  

这样,一套登录和鉴权的web系统就做好了,关键是性能好。

四、Openresty的扩展

使用lua-rest在github上搜索,会看到很多openresty的扩展,在apisix中,使用了很多扩展组件。使用 luarocks(Luarocks 是一个 Lua 包管理器,基于 Lua 语言开发,提供一个命令行的方式来管理 Lua 包依赖、安装第三方 Lua 包等)进行安装。

在这里插入图片描述
在这里插入图片描述

五、Openresty的使用场景

  1. 网关:kong、apisix等
  2. web框架:Lapis等
  3. waf(web应用防火墙):Naxsi等

所以,crud可以不用装jvm了(https://github.com/openresty/lua-resty-mysql)。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-08-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Openresty能帮我们做什么
  • 一、Openresty是什么
  • 二、Openresty怎么用
  • 三、Openresty怎么写
    • 3.1 登录示例
      • 3.2 鉴权示例
      • 四、Openresty的扩展
      • 五、Openresty的使用场景
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档