OpenResty 介绍

发布时间: 更新时间: 总字数:1174 阅读时间:3m 作者: 分享 复制网址

OpenResty 是一个全功能的 Web 高性能应用服务器,由标准的 Nginx 核心和众多第三方模块组成,通过 Lua 编程语言和 Nginx C 模块等组合,实现高性能的 Web 应用服务器

介绍

  • RestyREST 风格的意思
  • 支持通过 Lua 做复杂的扩展
  • 异步非阻塞,与 Lua 结合的性能接近 C module
  • 支持调用 Nginx C module
  • 工作原理
how openresty work

图片摘自

安装

源码编译

  • 准备
apt-get install -y libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make \
  build-essential zlib1g zlib1g-dev

wget https://openresty.org/download/openresty-1.19.9.1.tar.gz
tar xzvf openresty-1.19.9.1.tar.gz
cd openresty-1.19.9.1
  • 编译
# 编译 --with-Components 激活组件,--without 则是禁止组件
./configure -j4 --prefix=/opt/openresty \
    --with-luajit\
    --with-http_iconv_module

make -j4
make install

build 时指定各个模块,参考 http://nginx.org/en/docs/configure.html

  • 配置 PATH 路径
echo "export PATH=$PATH:/opt/openresty/nginx/sbin:/opt/openresty/bin/" >> /etc/profile
  • 版本
$ nginx -v
nginx version: openresty/1.19.9.1
$ openresty -v
nginx version: openresty/1.19.9.1

Ubuntu

# Import GPG Key
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -

# Add Openresty repository
sudo apt-get -y install software-properties-common
sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"

# Install Openresty
sudo apt-get update
sudo apt-get install openresty

Docker 镜像

docker run -it -d -p 8080:80 -p 8443:443 openresty/openresty:1.21.4.1-6-focal

使用介绍

目录结构

$ tree -L 1 /opt/openresty/
/opt/openresty/  # 主目录
├── bin    # 可执行文件目录,包括 openresty 启动 OpenResty 服务;opm 类似于 rpm、npm 的包管理工具;resty-cli 以命令行模式直接运行 Lua 程序;restydoc 参考帮助手册
├── COPYRIGHT
├── luajit  # luajit 运行库
├── lualib  # lua lib 组件
├── nginx   # nginx 核心
├── pod     # restydoc 的数据
├── resty.index
└── site    # 包管理工具 opm 的安装目录

$ tree -L 1 /opt/openresty/lualib/
/opt/openresty/lualib/
├── cjson.so
├── librestysignal.so
├── ngx  # ngx 模块
├── rds
├── redis
├── resty  # 包括如:http.lua/http_headers.lua/lrucache.lua/md5.lua/mysql.lua/redis.lua ...
└── tablepool.lua

4 directories, 3 files

服务管理

# 启动服务
nginx -p /opt/openresty/nginx

# 停止服务
nginx -p /opt/openresty/nginx/ -s stop

# 重新加载配置
nginx -p /opt/openresty/nginx/ -s reload

说明:

  • -c 指定配置文件,一般为 *.conf
  • -p path 指定配置文件路径
  • -s signal 指定信号量,signal 值可以是:
    • stop 强制停止
    • quit 处理完所有请求后停止服务
    • reload 重启服务
    • reopen 重新打开日志文件,服务不中断,用来处理 logrotate
  • -t 检查默认配置文件
  • -T 检查默认配置文件并打印输出
  • -v/-V 显示版本信息,-V 信息更完全

帮助 restydocs

restydoc nginx|luajit|opm|resty-cli|ngx_echo|ngx_lua|lua-cjson 等

restydoc -s proxy_pass                # 反向代理说明
restydoc -s content_by_lua_block      # content_by_lua_block说明
restydoc -s ngx.say
restydoc -s concat

# opm 安装的包,需要使用参数 `-r` 指定安装目录
restydoc -r /opt/openresty/site/xxx

命令行工具

# 执行lua代码
resty -e "print('Hello World!')"

也可以通过类似于 #!/bin/bash 一样,用在文件首行指定文件的解释器,格式 #!/opt/openresty/bin/resty

示例

  • /opt/openresty/nginx/conf/nginx.conf 中修改
    server {
        listen       80;
...
        location /hello-world {
            default_type text/html;

            content_by_lua_block {
                ngx.say("Hello World")
            }
        }
...
  • 重新加载配置
# 配置验证
nginx -t

# 重载
nginx -s reload
  • 验证
$ curl http://127.0.0.1/hello-world
Hello World

调试技巧

  • lua cache

关闭 lua_code_cache 时,OpenResty 会给每个请求创建新的 Lua VM。由于没有 Lua module 的缓存,新的 VM 会加载最新的 Lua 文件,配置如下:

http {
  lua_code_cache off;
  ...
}

说明:

  • init_by_lua_file/init_worker_by_lua_file 不生效

  • *_by_lua_block 不生效

  • 通过 nginx debug 日志显示结果

    • error_log /path/to/log debug;

配置

http {
  lua_use_default_type on|of  -- 发送相应的默认 Content-Type
  lua_malloc_trim num  -- 清理内存周期,num 是请求次数,默认为 1000,0 禁用清理内存
  lua_need_request_body on|off  -- 默认 off,是否在开始处理流程前强制读取请求体数据,不建议设置为 on,影响效率,通过 ngx.req.read_body 函数读取
  lua_http10_buffering on|off  -- 默认 on,http 1.0 缓冲,当前为 http 1.1 建议配置 off
  rewrite_by_lua_no_postpone on|off  -- 默认 off,rewrite_by_lua 最后执行
  access_by_lua_no_postpone on|off  -- access_by_lua 最后执行
  lua_transform_underscores_in_response_headers on|off  -- 默认 on,是否把 Lua 代码的相应头名字 _ 转化为 -
  lua_check_client_abort on|off  -- 默认 off,是否启用客户端意外断连检测
}

OpenResty WAF

参考

  1. https://openresty.org/en/
  2. https://github.com/moonbingbing/openresty-best-practices
  3. https://moonbingbing.gitbooks.io/openresty-best-practices/
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数