Skip to content
Leo的技术分享
Go back

OpenResty 入门教程

OpenResty 是一款基于 Nginx 和 Lua 的高性能 Web 框架,可以方便地基于 Nginx 进行二次开发,以实现超高并发 Web 网关,Web 服务等。

本文讲述如何在 Linux 安装和使用 OpenResty。

安装 OpenResty

OpenResty 官方提供源代码编译安装以及二进制包安装方式,本文采用源代码编译安装方式。有关二进制包安装方式,可以参考链接 https://openresty.org/cn/linux-packages.html

在 OpenResty 下载页面 下载最新版本的 OpenResty,这里笔者下载的是 openresty-1.19.3.1.tar.gz

然后依次执行命令:

tar zxvf openresty-1.19.3.1.tar.gz
cd openresty-1.19.3.1
./configure
make
make install

注意执行 make install 需要 root 权限。 可以看到 OpenResty 被默认安装到了 /usr/local/openresty/ 目录。

启动 Nginx

创建目录 openresty/home/lihao/code/openresty),接下来的实验都在这个目录进行,然后执行:

cd openresty
mkdir conf logs

其中,conf 目录用于存放 Nginx 配置文件,logs 目录用于存放 Nginx 日志文件。在 conf 目录创建 nginx.conf 文件:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 9080;
        location / {
            default_type text/html;
            content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}

其中 ngx.say("<p>hello, world</p>") 用于在浏览器输出 hello, world。 启动 Nginx:

cd /home/lihao/code/openresty
/usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c conf/nginx.conf

其中:

打开浏览器,地址栏输入 http://10.88.115.137:9080/,可以看到页面输出 hello, world

这时,进入 logs 目录,可以看到日志和 pid 文件:

logs/
├── access.log
├── error.log
└── nginx.pid

独立 Lua 源代码文件

上面的 Lua 代码直接写在 Nginx 配置文件里面,当代码多了后不好维护,可以将 Lua 代码保存成独立文件。

openresty 目录下创建 lua 目录,然后在 lua 目录创建 hello.lua 文件:

ngx.say("<p>hello, world from lua src</p>")

调整 nginx.conf 配置:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 9080;
        location ~ /lua/(.+) {
            default_type text/html;
            content_by_lua_file lua/$1.lua;
        }
    }
}

其中, location ~ /lua/(.+) 用于动态匹配 url,content_by_lua_file lua/$1.lua; 用于指明 Lua 文件位置。

重启 Nginx ,即,kill master 进程后执行:

/usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c conf/nginx.conf

打开浏览器,访问 http://10.88.115.137:9080/lua/hello,可以看到页面输出 hello, world from lua src

获取请求参数

接下来实现一个稍复杂一点的例子,提取客户端请求 name 参数,并打印出来。

创建 lua/req.lua 文件,提供 get_args 函数,用于提取请求参数:

local _M = {}
-- 获取 http get/post 请求参数
function _M.get_args()
    -- 获取 http 请求方式 get/post
    local request_method = ngx.var.request_method
    -- 这里是一个 table,包含所有 get 请求参数
    local args = ngx.req.get_uri_args()
    -- 如果是 post 参数获取
    if "POST" == request_method then
        -- 先读取请求体
        ngx.req.read_body()
        -- 这里也是一个 table,包含所有 post 请求参数
        local post_args = ngx.req.get_post_args()
        if post_args then
            for k, v in pairs(post_args) do
                args[k] = v
            end
        end
    end
    return args
end

return _M

创建 lua/test.lua ,通过调用 get_args 函数,提取 name 参数值:

-- 引入 req 模块
local req = require "req"

-- 获取请求参数列表
local args = req.get_args()

-- 获取 key 为 name 的值
local name = args['name']

-- 如果不存在指定默认值
if name == nil or name == "" then
    name = "leo"
end

-- 输出结果
ngx.say("<p>hello " .. name .. "!</p>")

修改 nginx.conf

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    lua_package_path "/home/lihao/code/openresty/lua/?.lua;;";
    server {
        listen 9080;
        location ~ /lua/(.+) {
            default_type text/html;
            content_by_lua_file lua/$1.lua;
        }
    }
}

其中,lua_package_path "/home/lihao/code/openresty/lua/?.lua;;"; 用于指定引入模块的路径。

重启 Nginx,打开浏览器,输入 http://10.88.115.137:9080/lua/test?name=Leo,可以看到输出 hello Leo!

参考资料


Share this post on:

Previous Post
Spring Boot 多线程使用
Next Post
Nginx 模块开发 Hello World