有勇气的牛排博客

nginx-resty入门安装使用教程

有勇气的牛排 600 nginx 2024-09-07 10:06:58

前言

NGINX Resty(也称为 OpenResty)是基于 NGINX 和 LuaJIT 的高性能 Web 平台。它扩展了 NGINX 的功能,使其不仅仅是一个 HTTP 服务器,还能执行复杂的 Web 应用程序逻辑。OpenResty 在 NGINX 之上整合了 Lua 脚本引擎,通过 Lua 脚本开发者可以直接在 NGINX 中编写自定义逻辑,从而处理复杂的请求和响应流程。

主要特性:

  1. Lua 支持:OpenResty 最大的特点就是内置了 LuaJIT,开发者可以用 Lua 编写高性能的 Web 应用,灵活处理请求、缓存、数据库连接等。通过 ngx_lua 模块可以轻松实现复杂的请求处理逻辑。
  2. 高性能:OpenResty 基于 NGINX 的事件驱动架构,结合 LuaJIT 的高效执行,具备极高的并发处理能力和低延迟特性,适合处理高负载场景。
  3. 模块丰富:OpenResty 集成了大量常用模块,例如 lua-resty-mysqllua-resty-redis 等,可以方便地与数据库、缓存等外部服务进行交互。
  4. 内建异步支持:通过 Lua,可以使用异步非阻塞的方式处理请求,这对提高应用的性能和吞吐量非常有帮助,尤其是在与外部系统进行 I/O 操作时(如数据库、文件系统)。
  5. 动态配置和脚本化:相比 NGINX 静态配置,OpenResty 允许通过 Lua 脚本实现动态配置和请求处理逻辑,可以根据具体的请求参数或上下文动态生成响应。
  6. 轻松扩展:OpenResty 提供了强大的扩展能力,可以根据业务需求开发自定义模块或功能,同时也可以轻松与已有的 NGINX 模块结合使用。

典型应用场景:

  • 动态代理:OpenResty 可以根据请求中的信息(如请求头、URL、Cookie 等)动态调整路由和代理逻辑。
  • API 网关:凭借其高性能和灵活的 Lua 脚本能力,OpenResty 常被用于构建 API 网关,通过自定义脚本实现限流、鉴权、日志记录等功能。
  • 缓存:可以结合 Redis 等外部缓存服务,通过 Lua 脚本灵活控制缓存策略,减少后端系统的压力。
  • 日志处理和监控:可以通过 Lua 实现复杂的日志处理和格式化逻辑,支持将日志数据发送到外部系统进行进一步处理和分析。

1 安裝下載

https://openresty.org/cn/download.html

centos

安裝基礎庫: perl 5.6.1+, libpcre, libssl

# ubuntu apt-get install libpcre3-dev apt-get install libssl-dev apt-get install perl apt-get install make apt-get install build-essential apt-get install curl # Fedora 和 RedHat 用户 yum install -y pcre-devel yum install -y openssl-devel yum install -y gcc yum install -y curl yum install -y zlib-devel

原创:有勇气的牛排
https://www.couragesteak.com/article/472

準備文件

cd /usr/local mkdir openresty tar -zxvf openresty-1.25.3.2.tar.gz cd openresty-1.25.3.2

構建

cd /usr/local/openresty/openresty-1.25.3.2
./configure --prefix=/usr/local/openresty/openresty_install \ --with-luajit \ --with-http_iconv_module \ --with-stream \ --with-stream_ssl_module # --with-luajit: 启用 LuaJIT 以支持高性能 Lua 脚本执行。 # --with-stream/--with-stream_ssl_module: 启用 Nginx 的 Stream 模块,以支持 TCP/UDP 负载均衡和处理。 # -- with-http_iconv_module: 处理多语言网站、旧系统兼容性:UTF-8、ISO-8859-1、GBK

安裝編譯

make && make install
cd /usr/local/openresty/openresty_install

启动

启动 start_openresty.sh

#!/bin/bash # OpenResty 安装路径 OPENRESTY_PATH="/usr/local/openresty/openresty_install/nginx" # 启动 OpenResty $OPENRESTY_PATH/sbin/nginx # 检查是否成功启动 if [ $? -eq 0 ]; then echo "OpenResty 启动成功." else echo "OpenResty 启动失败." fi

关闭: stop_openresty.sh

#!/bin/bash # OpenResty 安装路径 OPENRESTY_PATH="/usr/local/openresty/openresty_install/nginx" # 关闭 OpenResty $OPENRESTY_PATH/sbin/nginx -s stop # 检查是否成功关闭 if [ $? -eq 0 ]; then echo "OpenResty 关闭成功." else echo "OpenResty 关闭失败." fi

重载: reload_openresty.sh

#!/bin/bash # OpenResty 安装路径 OPENRESTY_PATH="/usr/local/openresty/openresty_install/nginx" # 重载 OpenResty 配置 $OPENRESTY_PATH/sbin/nginx -s reload # 检查是否成功重载 if [ $? -eq 0 ]; then echo "OpenResty 配置重载成功." else echo "OpenResty 配置重载失败." fi
chmod +x start_openresty.sh chmod +x stop_openresty.sh chmod +x reload_openresty.sh

访问:

http://192.168.56.20/

基础库安装

luarocks install lua-resty-mysql luarocks install lua-cjson luarocks install lua-resty-http

2 HTTP方法常量

ngx.HTTP_GET ngx.HTTP_HEAD ngx.HTTP_PUT ngx.HTTP_POST ngx.HTTP_DELETE ngx.HTTP_OPTIONS (v0.5.0rc24 版本加入) ngx.HTTP_MKCOL (v0.8.2 版本加入) ngx.HTTP_COPY (v0.8.2 版本加入) ngx.HTTP_MOVE (v0.8.2 版本加入) ngx.HTTP_PROPFIND (v0.8.2 版本加入) ngx.HTTP_PROPPATCH (v0.8.2 版本加入) ngx.HTTP_LOCK (v0.8.2 版本加入) ngx.HTTP_UNLOCK (v0.8.2 版本加入) ngx.HTTP_PATCH (v0.8.2 版本加入) ngx.HTTP_TRACE (v0.8.2 版本加入)

3 HTTP状态常量

value = ngx.HTTP_OK (等于 200) value = ngx.HTTP_CREATED (等于 201) value = ngx.HTTP_SPECIAL_RESPONSE (等于 300) value = ngx.HTTP_MOVED_PERMANENTLY (等于 301) value = ngx.HTTP_MOVED_TEMPORARILY (等于 302) value = ngx.HTTP_SEE_OTHER (等于 303) value = ngx.HTTP_NOT_MODIFIED (等于 304) value = ngx.HTTP_BAD_REQUEST (等于 400) value = ngx.HTTP_UNAUTHORIZED (等于 401) value = ngx.HTTP_FORBIDDEN (等于 403) value = ngx.HTTP_NOT_FOUND (等于 404) value = ngx.HTTP_NOT_ALLOWED (等于 405) value = ngx.HTTP_GONE (等于 410) value = ngx.HTTP_INTERNAL_SERVER_ERROR (等于 500) value = ngx.HTTP_METHOD_NOT_IMPLEMENTED (等于 501) value = ngx.HTTP_SERVICE_UNAVAILABLE (等于 503) value = ngx.HTTP_GATEWAY_TIMEOUT (等于 504) (v0.3.1rc38 版本加入)

4 语法API

2.1 ngx.say

返回结果给客户端

语法:ngx.say("")

location / {
    default_type 'text/plain';
    content_by_lua_block {
        ngx.say("牛逼")
    }
}

image.png

2.2 ngx.print

location / { default_type 'text/plain'; content_by_lua_block { # 作为响应体返回 local table_data = { 'id' } ok, err = ngx.print(table_data) ngx.say("有勇氣的牛排") } }

image.png

2.3 ngx.flush

定义:用户强制将nginx的输出缓冲区的内容,立即发送给客户端。

通常在调用 ngx.print 后使用,确保数据及时传递到客户端。

场景:

  • 实时发送数据流
  • 长轮训
  • 分块传输编码(chunked transfer encoding)
ngx.print('...') ngx.flush(true)

参数:

  • wait:可选
  • true:表示将等待数据发送完成后再继续处理

注意:

  • 这会增加网络开销,因为它会打破数据包的聚合,从而导致更多的TCP包发送。

  • 会影响性能。

2.4 ngx.arg

2.5 ngx.var 获取nginx变量

获取值

local uri = ngx.var.uri -- 获取当前请求的 URI local uri = ngx.var.request_method -- 当前请求的方法GET、POST等 local uri = ngx.var.args -- 当前请求的查询参数 local uri = ngx.var.remote_addr -- 客户端的 IP 地址

设置自定义变量值(如果nx)

ngx.var.some_variable = "new_value"

注意:

  • ngx中部分变量为只读

  • 在设置变量时,如果ngx没有定义,则可能报错

2.6 ngx.log 输出日志

ngx.STDERR -- 标准错误输出,用于记录严重错误 ngx.EMERG -- 紧急情况,表示系统不可用 ngx.ALERT -- 需要立即采取行动的错误 ngx.CRIT -- 严重的错误 ngx.ERR -- 一般错误 ngx.WARN -- 警告信息,可能存在问题 ngx.NOTICE -- 普通通知信息,表示状态变化 ngx.INFO -- 一般信息,记录应用程序的正常操作 ngx.DEBUG -- 调试信息,记录程序细节

使用

ngx.log(ngx.ERR, '异常: ', '错误信息ERR') ngx.log(ngx.INFO, 'IP地址: ', ngx.var.remote_addr) ngx.log(ngx.DEBUG, 'URI地址为: ', ngx.var.uri)

输出结果在:error.log文件中

image.png

如果看不到结果,请配置

# nginx.conf  第7行左右
error_log  logs/error.log  debug;

2.7 ngx.ctx

location里面的全局变量/上下文变量

ngx.exit

ngx.sleep

ngx.sleep(1000)

5 请求API

arg 参数

GET 参数

http://127.0.0.1/?id=1&name=有勇气的牛排

ngx.req.get_post_args: V1.25.3.2版本已经无法使用

老方法(不知道哪个版本在用)

-- 获取 URL 参数 local args = ngx.req.get_url_args() -- 获取具体的参数 local id = args["id"] lcoal age = args["name"] -- 输出参数 ngx.say("昵称:", name) ngx.say("ID:", id)

新语法:V1.25.3.2语法

-- 获取 URL 参数 local id = ngx.var.arg_id local name = ngx.var.arg_name -- 输出参数 ngx.say("昵称:", name) ngx.say("ID:", id)

POST参数

ngx.req.get_post_args: V1.25.3.2版本已经无法使用

-- 获取 URL 参数(GET/POST) local id = ngx.var.arg_id local name = ngx.var.arg_name -- 输出参数 ngx.say("昵称:", name) ngx.say("ID:", id)

method 方法

ngx.req.get_method()

local http_method = ngx.req.get_method() ngx.say("方法:", http_method)

image.png

ngx.req.set_method

ngx.req.set_method(ngx.HTTP_GET)

header操作

添加、修改、清除请求头

-- 2种語法 ngx.header.content_type = 'text/plain'; ngx.header["My-Header"] = '有勇气的牛排'

image.png

重定向 跳转

ngx.redirect

注意:跳转第三方网站添加 https

http://127.0.0.1/
-- 3种语法 return ngx.redirect("/test") return ngx.redirect("/test", 301) return ngx.redirect("https://www.couragesteak.com/", ngx.HTTP_MOVED_TEMPORARILY)

ngx.exec

内部跳转

指令 API

init_by_lua

Master 进程被创建时执行

该指令在 nginx 重新加载配置时执行,可以将耗时的模块提前加载,或者初始化全局变量。

即:公共模块

lua_by_lua_block { -- 全局导入 MySQL 和 Redis mysql = require "resty.mysql" redis = require "resty.redis" my_global_var = "这是一个全局变量" }

image.png

可以导入,也可以直接写

init_worker_by_lua

每个 Worker 进程(工作进程)被创建时执行

content_by_lua

编译配置

[root@master openresty-1.25.3.2]# ./configure --help --help 显示此帮助信息 --prefix=PATH 设置安装前缀目录(默认路径为 /usr/local/openresty) --with-debug 启用调试日志记录 --with-no-pool-patch 启用 no-pool 补丁以调试内存问题 -jN 在构建 LuaJIT 2.1 时传递 -jN 选项 --without-http_echo_module 禁用 ngx_http_echo_module --without-http_xss_module 禁用 ngx_http_xss_module --without-http_coolkit_module 禁用 ngx_http_coolkit_module --without-http_set_misc_module 禁用 ngx_http_set_misc_module --without-http_form_input_module 禁用 ngx_http_form_input_module --without-http_encrypted_session_module 禁用 ngx_http_encrypted_session_module --without-http_srcache_module 禁用 ngx_http_srcache_module --without-http_lua_module 禁用 ngx_http_lua_module --without-http_lua_upstream_module 禁用 ngx_http_lua_upstream_module --without-http_headers_more_module 禁用 ngx_http_headers_more_module --without-http_array_var_module 禁用 ngx_http_array_var_module --without-http_memc_module 禁用 ngx_http_memc_module --without-http_redis2_module 禁用 ngx_http_redis2_module --without-http_redis_module 禁用 ngx_http_redis_module --without-http_rds_json_module 禁用 ngx_http_rds_json_module --without-http_rds_csv_module 禁用 ngx_http_rds_csv_module --without-stream_lua_module 禁用 ngx_stream_lua_module --without-ngx_devel_kit_module 禁用 ngx_devel_kit_module --without-stream 禁用 TCP/UDP 代理模块 --without-http_ssl_module 禁用 ngx_http_ssl_module --without-stream_ssl_module 禁用 ngx_stream_ssl_module --with-http_iconv_module 启用 ngx_http_iconv_module --with-http_drizzle_module 启用 ngx_http_drizzle_module --with-http_postgres_module 启用 ngx_http_postgres_module --without-lua_cjson 禁用 lua-cjson 库 --without-lua_tablepool 禁用 lua-tablepool 库(因此也禁用 lua-resty-shell 库) --without-lua_redis_parser 禁用 lua-redis-parser 库 --without-lua_rds_parser 禁用 lua-rds-parser 库 --without-lua_resty_dns 禁用 lua-resty-dns 库 --without-lua_resty_memcached 禁用 lua-resty-memcached 库 --without-lua_resty_redis 禁用 lua-resty-redis 库 --without-lua_resty_mysql 禁用 lua-resty-mysql 库 --without-lua_resty_upload 禁用 lua-resty-upload 库 --without-lua_resty_upstream_healthcheck 禁用 lua-resty-upstream-healthcheck 库 --without-lua_resty_string 禁用 lua-resty-string 库 --without-lua_resty_websocket 禁用 lua-resty-websocket 库 --without-lua_resty_limit_traffic 禁用 lua-resty-limit-traffic 库 --without-lua_resty_lock 禁用 lua-resty-lock 库 --without-lua_resty_lrucache 禁用 lua-resty-lrucache 库 --without-lua_resty_signal 禁用 lua-resty-signal 库(因此也禁用 lua-resty-shell 库) --without-lua_resty_shell 禁用 lua-resty-shell 库 --without-lua_resty_core 禁用 lua-resty-core 库 --with-luajit=DIR 使用由 DIR 指定的外部 LuaJIT 2.1 安装 --with-luajit-xcflags=FLAGS 为 LuaJIT 2.1 指定额外的 C 编译器标志 --with-luajit-ldflags=FLAGS 为 LuaJIT 2.1 指定额外的 C 链接器标志 --without-luajit-lua52 禁用 LuaJIT 5.2 的扩展,以防止破坏向后兼容性 --without-luajit-gc64 禁用 LuaJIT 的 GC64 模式(在 x86_64 上默认启用) --with-libdrizzle=DIR 指定 libdrizzle 1.0(或 drizzle)安装前缀 --with-libpq=DIR 指定 libpq(或 postgresql)安装前缀 --with-pg_config=PATH 指定 pg_config 工具的路径 Nginx 的选项继承: --sbin-path=PATH 设置 nginx 二进制文件的路径 --modules-path=PATH 设置模块路径 --conf-path=PATH 设置 nginx.conf 文件路径 --error-log-path=PATH 设置错误日志文件路径 --pid-path=PATH 设置 nginx.pid 文件路径 --lock-path=PATH 设置 nginx.lock 文件路径 --user=USER 设置 worker 进程的非特权用户 --group=GROUP 设置 worker 进程的非特权用户组 --build=NAME 设置构建名称 --builddir=DIR 设置构建目录 --with-select_module 启用 select 模块 --without-select_module 禁用 select 模块 --with-poll_module 启用 poll 模块 --without-poll_module 禁用 poll 模块 --with-threads 启用线程池支持 --with-file-aio 启用文件 AIO 支持 --with-http_ssl_module 启用 ngx_http_ssl_module(默认启用) --with-http_v2_module 启用 ngx_http_v2_module --with-http_v3_module 启用 ngx_http_v3_module --with-http_realip_module 启用 ngx_http_realip_module --with-http_addition_module 启用 ngx_http_addition_module --with-http_xslt_module 启用 ngx_http_xslt_module --with-http_xslt_module=dynamic 启用动态 ngx_http_xslt_module --with-http_image_filter_module 启用 ngx_http_image_filter_module --with-http_image_filter_module=dynamic 启用动态 ngx_http_image_filter_module --with-http_geoip_module 启用 ngx_http_geoip_module --with-http_geoip_module=dynamic 启用动态 ngx_http_geoip_module --with-http_sub_module 启用 ngx_http_sub_module --with-http_dav_module 启用 ngx_http_dav_module --with-http_flv_module 启用 ngx_http_flv_module --with-http_mp4_module 启用 ngx_http_mp4_module --with-http_gunzip_module 启用 ngx_http_gunzip_module --with-http_gzip_static_module 启用 ngx_http_gzip_static_module --with-http_auth_request_module 启用 ngx_http_auth_request_module --with-http_random_index_module 启用 ngx_http_random_index_module --with-http_secure_link_module 启用 ngx_http_secure_link_module --with-http_degradation_module 启用 ngx_http_degradation_module --with-http_slice_module 启用 ngx_http_slice_module --with-http_stub_status_module 启用 ngx_http_stub_status_module --without-http_charset_module 禁用 ngx_http_charset_module --without-http_gzip_module 禁用 ngx_http_gzip_module --without-http_ssi_module 禁用 ngx_http_ssi_module --without-http_userid_module 禁用 ngx_http_userid_module --without-http_access_module 禁用 ngx_http_access_module --without-http_auth_basic_module 禁用 ngx_http_auth_basic_module --without-http_mirror_module 禁用 ngx_http_mirror_module --without-http_autoindex_module 禁用 ngx_http_autoindex_module --without-http_geo_module 禁用 ngx_http_geo_module --without-http_map_module 禁用 ngx_http_map_module --without-http_split_clients_module 禁用 ngx_http_split_clients_module --without-http_referer_module 禁用 ngx_http_referer_module --without-http_rewrite_module 禁用 ngx_http_rewrite_module --without-http_proxy_module 禁用 ngx_http_proxy_module --without-http_fastcgi_module 禁用 ngx_http_fastcgi_module --without-http_uwsgi_module 禁用 ngx_http_uwsgi_module --without-http_scgi_module 禁用 ngx_http_scgi_module --without-http_grpc_module 禁用 ngx_http_grpc_module --without-http_memcached_module 禁用 ngx_http

留言

专栏
文章
加入群聊