Ken的杂谈
  • Ken的杂谈 (current)
  • 关于
  • 杂谈
    Java Spring Spring Boot Spring Cloud MyBatis C# .NET Core .NET ASP.NET Core ASP.NET ClassLibrary Mono 数据库 MySQL SQL Server 网络 缓存 Web Linux CentOS Ubuntu macOS Windows openEuler Nginx ServiceStack JS/JQ 正则 工具 IDE Grafana Exceptions CI/CD Solution 微服务 Arch Docker 杂谈
  • 系列
    Java 快速入门系列教程 Spring Boot 入门教程 Spring Boot 2.x 入门教程 Spring Cloud 入门教程 .NET Core 快速入门教程 ASP.NET Core 2.1 入门教程 CentOS 7 快速上手教程 Ubuntu快速上手入门教程 Hyper-V基础教程 Docker入门教程
  • GitHub

在macOS上安装配置OpenResty

Nginx macOS @ 2024-04-27 19:33:56 · 阅读:(4017)

一、前言

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

image.png

三、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!

image.png

四、备注

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

2、引用/参考

  • https://openresty.org/cn/
  • https://openresty.org/cn/getting-started.html
  • https://ken.io/note/openresty-install-setup-linux

Ken的杂谈

本文由 ken.io 创作,采用CC BY 3.0 CN协议 进行许可。 可自由转载、引用、甚至修改,但需署名作者且注明出处。

Nginx macOS

随笔目录


    © Copyright 2012-2025 Ken的杂谈

    豫ICP备10025012号

    ASP.NET Core(6.0) on Linux