前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在macOS上安装配置OpenResty

在macOS上安装配置OpenResty

作者头像
KenTalk
发布2024-04-28 09:26:32
1240
发布2024-04-28 09:26:32
举报
文章被收录于专栏:Ken的杂谈Ken的杂谈

一、前言

?

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,参考:/developer/article/2259803

代码语言:javascript
复制
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

如果已经安装Homebrew,请更新

代码语言:javascript
复制
brew update

2、安装OpenResty

代码语言:javascript
复制
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、安装验证&启动

代码语言:javascript
复制
# 查看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
image.png

三、OpenResty配置

通过Homebrew安装的OpenResty,默认目录在/opt/homebrew/etc/openresty,默认配置文件为nginx.conf

1、新增配置目录

代码语言:javascript
复制
#1、新增配置文件夹
mkdir -p ~/openresty/conf

#2、修改默认配置
vi /opt/homebrew/etc/openresty/nginx.conf 

#3、在http属性下新增配置文件夹(绝对路径):
include /Users/ken/openresty/conf/*.conf;

2、基本转发配置

跟Nginx反向代理配置方式一致

代码语言:javascript
复制
#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

代码语言:javascript
复制
# 修改hosts配置
sudo vi /etc/hosts

# 增加配置
127.0.0.1 test.local.ken.io

使用curl命令或者浏览器进行访问测试

代码语言:javascript
复制
curl test.local.ken.io

3、使用Lua响应请求

监听8888端口,使用OpenResty内置的Lua函数响应请求,输出:Hello,{name}

代码语言:javascript
复制
#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命令或者浏览器进行访问测试

代码语言:javascript
复制
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
image.png

四、备注

1、OpenResty常用命令

代码语言:javascript
复制
# 启动 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、引用/参考

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
    • 1、本文主要内容
      • 2、本文环境信息
      • 二、OpenResty安装
        • 1、安装Homebrew
          • 2、安装OpenResty
            • 3、安装验证&启动
            • 三、OpenResty配置
              • 1、新增配置目录
                • 2、基本转发配置
                  • 3、使用Lua响应请求
                  • 四、备注
                    • 1、OpenResty常用命令
                      • 2、引用/参考
                      相关产品与服务
                      腾讯云服务器利旧
                      云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                      http://www.vxiaotou.com