OpenResty安装(Mac)和测试(hello world)

OpenResty安装(Mac)和测试(hello world)

介绍

OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。本文主要是对openresty在mac系统上进行安装,安装完成后开发简单的hello world应用程序进行简单测试。

安装

Mac系统上快速安装:

brew install openresty/brew/openresty
复制代码

安装过程如果遇到如下错误提示:

curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
复制代码

可参考 Mac 上重新安装homebrew的处理方式。

当然你可以使用源码包的方式进行安装。

我们使用openresty命令使用默认配置启动

openresty
复制代码

我们使用openresty来启动应用,openresty命令的具体说明可以使用下面命令查看:

openresty -h
复制代码

输出如下:

nginx version: openresty/1.19.9.1

Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]

             [-e filename] [-c filename] [-g directives]

\


Options:

  -?,-h         : this help

  -v            : show version and exit

  -V            : show version and configure options then exit

  -t            : test configuration and exit

  -T            : test configuration, dump it and exit

  -q            : suppress non-error messages during configuration testing

  -s signal     : send signal to a master process: stop, quit, reopen, reload

  -p prefix     : set prefix path (default: /usr/local/Cellar/openresty/1.19.9.1_2/nginx/)

  -e filename   : set error log file (default: /usr/local/var/log/nginx/error.log)

  -c filename   : set configuration file (default: /usr/local/etc/openresty/nginx.conf)

  -g directives : set global directives out of configuration file
复制代码

启动后我们在浏览器输入http://127.0.0.1 会看到如下页面:

image.png

我们可以使用OpenResty附带的resty命令行工具来简单测试下:

resty -e 'print("Hello World")'
复制代码

输出为:

Hello World
复制代码

我们已经安装成功,下面我们来编写简单的hello world项目

hello world项目

首先我们创建一个hello-world目录并在下面创建logs、conf、 lua目录

# mkdir hello-world
# cd hello-world/
# mkdir logs conf lua
复制代码

其中lua/ 目录来存放我们的 lua 模块文件.

接下来我们编辑lua模块文件:

vi /lua/hello.lua
复制代码

一个简单的hello.lua的lua模块内容为:

local _M = {}
function _M.welcome(name)
    ngx.say("Hello World! ", name)
end
return _M
复制代码

接着我们来编辑nginx.conf配置:

vi /conf/nginx.conf
复制代码

nginx.conf如下:

worker_processes 1;
events {
    worker_connections 1024;
}
http {
    lua_package_path "$prefix/lua/?.lua;;";
    server {
        listen 8080 reuseport;
        location / {
            default_type text/plain;
            content_by_lua_block {
                
                local hello = require "hello"
                hello.welcome("wk")
            }
        }
    }
}
复制代码

其中特殊变量 $prefix 在运行时被 nginx 的 -p 选项值所替代;require 内置函数加载 Lua 模块 hello

配置好以后我们来看下目录树:

tree .
复制代码
.

├── conf

    └── nginx.conf

├── logs

└── lua

    └── hello.lua
3 directories, 1 file
复制代码

先来检查下我们配置的nginx.conf是否正确:

openresty -p $PWD/ -t -c conf/nginx.conf
复制代码
nginx: the configuration file /Users/wk/software/hello-world/conf/nginx.conf syntax is ok

nginx: configuration file /Users/wk/software/hello-world/conf/nginx.conf test is successful
复制代码

看来是没问题的。

那么我们来启动一下刚才创建的OpenResty应用程序(在hello-world目录下)。

openresty -p $PWD/  -c conf/nginx.conf
复制代码
  • -p 的含义为指定项目的工作目录

  • -c 指定nginx配置

此时我们在浏览器输入:http://127.0.0.1:8080/ 会展示:Hello World! wk

image.png

目前为止一个简单的hello-world项目就算完成了。

我们发现Lua 模块是在这第一个请求中加载的,后续的请求只是使用内存中缓存的 Lua 模块,所以我们可以使用init_by_lua_block指令来在OpenResty启动的时候就把lua模块加载到内存。

init_by_lua_block

init_by_lua_block指令会在OpenResty启动的时候就把lua模块加载到内存

我们可以使用restydoc 命令来查看init_by_lua_block 的具体说明(其它类似)。

restydoc -s init_by_lua_block
复制代码

输出如下:

   init_by_lua_block
       syntax: init_by_lua_block { lua-script }

       context: http

       phase: loading-config

       Similar to the init_by_lua directive except that this directive inlines
       the Lua source directly inside a pair of curly braces ("{}") instead of
       in an Nginx string literal (which requires special character escaping).

       For instance,

            init_by_lua_block {
                print("I need no extra escaping here, for example: \r\nblah")
            }

       This directive was first introduced in the "v0.9.17" release.



OpenResty                         2022-02-21                ngx_lua-0.10.20(7)
复制代码

我们修改例子来实现下,修改hello-world项目下面的conf/nginx.conf为:

worker_processes 1;
events {
    worker_connections 1024;
}
http {
    init_by_lua_block {
            require "hello"
    }
    lua_package_path "$prefix/lua/?.lua;;";
    server {
        listen 8080 reuseport;
        location / {
            default_type text/plain;
            content_by_lua_block {
                local hello = require "hello"
                hello.welcome("wk")
            }
        }
    }
}
复制代码

我们在浏览器输入:http://127.0.0.1:8080/ 会展示:Hello World! wk。

这样服务器启动时预加载 Lua 模块可以避免第一次请求的额外开销

上面的nginx.conf我们可以看到lua代码时放在了配置文件里面进行编码,那么我们是不是可以把它单独拿出来那,下面我们来看下content_by_lua_file

content_by_lua_file

使用content_by_lua_file可以将lua代码从nginx.conf配置独立出来。

例如我们将nginx.conf修改为:

worker_processes 1;

events {

    worker_connections 1024;

}

http {

    lua_package_path "$prefix/lua/?.lua;;";

    server {

        listen 8080 reuseport;

        location / {

            default_type text/plain;

            content_by_lua_file lua/welcome.lua;

        }

    }

}
复制代码

其中lua/welcome.lua为相对路径(也可设置lua绝对路径),内容如下:

local hello = require "hello"
hello.welcome("wk")
复制代码

此时直接在浏览器请求http://127.0.0.1:8080/ 会展示:Hello World! wk,可正常运行。

以上lua代码都会加载到缓存中,后期使用直接会取缓存。如果我们想要修改lua代码实时生效那,不加载到缓存,那么我们可以设置 lua_code_cache为off

lua_code_cache

设置 lua_code_cache为off,可使lua代码实时生效

修改conf/nginx.conf:

worker_processes 1;
events {
    worker_connections 1024;
}
http {
    init_by_lua_block {
            require "hello"
    }
    lua_package_path "$prefix/lua/?.lua;;";
    server {
        listen 8080 reuseport;
        location / {
            default_type text/plain;
            lua_code_cache off;
            content_by_lua_file lua/welcome.lua;
        }
    }
}
复制代码

然后重新加载:

openresty -s reload
复制代码

此时修改lua/hello.lua为:

local _M = {}

function _M.welcome(name)

    ngx.say("I love China! wk! ", name)

end

return _M
复制代码

此时直接在浏览器请求http://127.0.0.1:8080/ 会展示:I love China! wk,看来修改的lua代码实时生效了。

总结

本文介绍了OpenResty在MAC系统的安装以及实现了一个简单的hello world应用。此外,还举例介绍了init_by_lua_block,content_by_lua_file,lua_code_cache指令。

参考

blog.openresty.com.cn/cn/or-lua-m…

分类:
后端
标签: