一、前言
OpenResty是一个基于 Nginx 与 Lua 的开源高性能 Web 平台,OpenResty团队为Nginx开发了Lua模块,使得开发者/运维可以使用Lua为OpenResty开发扩展,或者为Nginx定制功能,另外OpenResty团队也内置了很多Lua扩展(JWT、MySQL、Redis等),可以通过OpenResty高效率的开发高性能Web服务
1、本文主要内容
- 使用Homebrew安装OpenResty并配置开机启动
- 使用OpenResty配置HTTP代理
- 使用OpenResty+Lua响应HTTP请求
- 常用OpenResty命令介绍
2、本文环境信息
| 工具/环境 | 版本说明 | 适用版本 | 
|---|---|---|
| macOS | 14.1.2 | 11+ | 
| Homebrew | 4.2 | 2.7+ | 
| OpenResty | 1.25.3.1 | 1.17+ | 
二、OpenResty安装
1、安装Homebrew
使用命令安装Homebrew,参考:https://ken.io/note/macos-homebrew-install-and-configuration
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
如果已经安装Homebrew,请更新
brew update
2、安装OpenResty
brew install openresty
# 输出示例
==> openresty
You can find the configuration files for openresty under /opt/homebrew/etc/openresty/.
To start openresty/brew/openresty now and restart at startup:
  sudo brew services start openresty/brew/openresty
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/openresty/bin/openresty -g daemon\ off\;
3、安装验证&启动
# 查看openresty版本
openresty -v
# 输出示例
nginx version: openresty/1.25.3.1
# 启用Homebrew的服务管理
brew tap homebrew/services
# 启动openresty
sudo brew services start openresty
# 输出示例
#忽略警告
==> Successfully started `openresty` (label: homebrew.mxcl.openresty)
# curl访问测试
curl localhost --head | grep Server
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  125k    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
Server: openresty/1.25.3.1
 
三、OpenResty配置
通过Homebrew安装的OpenResty,默认目录在/opt/homebrew/etc/openresty,默认配置文件为nginx.conf
1、新增配置目录
#1、新增配置文件夹
mkdir -p ~/openresty/conf
#2、修改默认配置
vi /opt/homebrew/etc/openresty/nginx.conf 
#3、在http属性下新增配置文件夹(绝对路径):
include /Users/ken/openresty/conf/*.conf;
2、基本转发配置
跟Nginx反向代理配置方式一致
#1、新建/修改配置文件
vi ~/openresty/conf/ken.conf
#2、配置内容
server {
    listen       80;        #监听80端口
    server_name  test.local.ken.io; #监听的域名
    location / {            #转发或处理
        proxy_pass https://ken.io;
    }
}
#3、重载配置
openresty -s reload
修改hosts
# 修改hosts配置
sudo vi /etc/hosts
# 增加配置
127.0.0.1 test.local.ken.io
使用curl命令或者浏览器进行访问测试
curl test.local.ken.io
3、使用Lua响应请求
监听8888端口,使用OpenResty内置的Lua函数响应请求,输出:Hello,{name}
#1、新建/修改配置文件
vi ~/openresty/conf/hello.conf
#2、配置内容
server {
    listen 8888;
    location / {
        default_type 'text/plain';
        content_by_lua_block {
            local args = ngx.req.get_uri_args()
            local name = args["name"]
            ngx.header["X-Header"] = "ken.io"
            if name then
                ngx.say("Hello, " .. name)
            else
                ngx.say("Hello, OpenResty!")
            end
        }
    }
}
#3、重载配置
openresty -s reload
使用curl命令或者浏览器进行访问测试
curl -i localhost:8888
curl -i localhost:8888\?name=Ken
# 输出示例
HTTP/1.1 200 OK
Server: openresty/1.25.3.1
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
X-Header: ken.io
Hello, OpenResty!
 
四、备注
1、OpenResty常用命令
# 启动 OpenResty 主进程
openresty
# 停止 OpenResty
openresty -s stop
# 优雅地关闭 OpenResty
openresty -s quit
# 重载 OpenResty 配置文件
openresty -s reload
# 重新打开日志文件
openresty -s reopen
# 显示 OpenResty 的版本信息
openresty -v
# 指定 OpenResty 工作目录
openresty -p /path/to/work_dir
# 使用指定的配置文件启动 OpenResty
openresty -c /path/to/nginx.conf
