OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
openresty 服务编译部署
服务编译
首先,可以根据下面的示例安装和构建OpenResty。
1 | 1. 安装所有依赖 |
优化:隐藏openresty版本号
1 | http { |
公网 + 监控 + Lua 场景的编译参数说明
| 参数 | 作用 |
|---|---|
--with-luajit | SkyWalking Nginx-Lua、Prometheus Lua 客户端都依赖 LuaJIT,性能差 5~10 倍 |
--with-pcre-jit | 公网请求正则匹配(路由、WAF、日志解析)极多,JIT 必开 |
--with-http_ssl_module | 公网强制 HTTPS |
--with-http_v2_module | 现代浏览器/客户端走 h2,需配合 SSL |
--with-http_realip_module | 前面有 CDN/SLB 时必须拿真实客户端 IP,监控和限流都靠它 |
--with-http_stub_status_module | 最轻量的 Nginx 自身状态(连接数/请求数),Prometheus nginx-prometheus-exporter 可抓,或自己 Lua 暴露 |
--with-threads + --with-file-aio | 公网网关若兼做静态资源/大响应体,线程池避免阻塞 event loop;纯反向代理不命中文件 IO 也无害 |
--with-http_gzip_static_module | 预压缩 .gz 直发,省 CPU |
--with-http_sub_module | 响应体脱敏(如把内部报错信息替换掉再返回公网) |
--with-http_auth_request_module | 微服务网关统一鉴权子请求 |
--with-stream* + ssl_preread | 公网 TCP/TLS 透传、按 SNI 路由、stream 层真实 IP |
--with-compat | 以后要装动态模块(如 brotli)不冲突 |
--user=nginx --group=nginx | 不以 root 跑 worker,降攻击面 |
-fstack-protector-strong 等 | 编译期栈保护,公网边界机建议开 |
采集 openresty 监控数据
配置 SkyWalking 链路追踪
下载拿源码(不需编译)
1 | cd /usr/local/openresty |
目录:/opt/openresty/lua/skywalking-nginx-lua/lib/?.lua
nginx.conf http 块加
1 | http { |
重启
openresty即可,链路数据走 HTTP 上报到 OAP,无需重编。
配置 Prometheus(nginx-lua-prometheus)
下载 Lua 库(不需编译)
1 | cd /opt/openresty/lua/ |
目录:/opt/openresty/lua/skywalking-nginx-lua/lib/?.lua
nginx.conf http 块加
1 | http { |
Prometheus 侧抓 http://openresty:9145/metrics 即可。
openresty 相关参数
以下是整理的 OpenResty ./configure 全量参数清单(基于 1.25.3.x / 1.27.x,覆盖 Nginx 原生参数 + OpenResty 扩展参数),按类别整理。
基础与路径类(Nginx 原生)
| 参数 | 说明 |
|---|---|
--help / -h | 打印帮助 |
--prefix=PATH | 安装根目录,默认 /usr/local/openresty |
--sbin-path=PATH | nginx 二进制路径 |
--conf-path=PATH | nginx.conf 路径 |
--error-log-path=PATH | 错误日志路径 |
--pid-path=PATH | pid 文件路径 |
--lock-path=PATH | lock 文件路径 |
--user=NAME | worker 运行用户(建议 nginx) |
--group=NAME | worker 运行组 |
--build=NAME | 自定义 build 名(nginx -V 可见) |
--builddir=PATH | 编译临时目录 |
编译工具链类
| 参数 | 说明 |
|---|---|
--with-cc=PATH | 指定 C 编译器 |
--with-cc-opt="..." | C 编译选项(如 -O2 -fstack-protector-strong) |
--with-ld-opt="..." | 链接选项(如 -Wl,-z,relro,-z,now) |
--with-debug | 开启 debug 日志(性能下降,排错用) |
--with-no-pool-patch | 关闭 nginx 内存池,排查 Lua 内存泄漏用 |
--with-dtrace-probes | 开启 DTrace USDT 探针 |
--with-dtrace=PATH | 指定 dtrace 路径 |
-jN | 并行编译(传给 make) |
依赖库静态编译类
| 参数 | 说明 |
|---|---|
--with-pcre=DIR | 指定 PCRE 源码目录(静态编入) |
--with-pcre-jit | 开启 PCRE JIT(正则性能关键) |
--with-pcre-opt=OPTS | PCRE 编译附加选项 |
--with-openssl=DIR | 指定 OpenSSL 源码目录 |
--with-openssl-opt=OPTS | OpenSSL 编译附加选项 |
--with-zlib=DIR | 指定 zlib 源码目录 |
--with-zlib-opt=OPTS | zlib 编译附加选项 |
--with-libdrizzle=DIR | drizzle 库路径(mysql 客户端模块用) |
--with-libpq=DIR | PostgreSQL 库路径 |
--with-pg_config=PATH | pg_config 路径 |
生产建议:能用系统
pcre-devel/openssl-devel/zlib-devel就用系统包;离线环境才用--with-xxx=源码目录。
Nginx 核心 HTTP 模块(默认开,可 --without- 关)
| 参数 | 说明 |
|---|---|
--without-http | 彻底关闭 HTTP 服务 |
--without-http-cache | 关闭 upstream 缓存 |
--without-http_charset_module | 字符集转换 |
--without-http_gzip_module | gzip 输出 |
--without-http_ssi_module | SSI 包含 |
--without-http_userid_module | userid 下发 |
--without-http_access_module | allow/deny |
--without-http_auth_basic_module | basic 认证 |
--without-http_mirror_module | 流量镜像 |
--without-http_rewrite_module | rewrite(关掉后 PCRE 仍可被其他模块用) |
--without-http_proxy_module | proxy_pass |
--without-http_fastcgi_module | FastCGI |
--without-http_uwsgi_module | uWSGI |
--without-http_scgi_module | SCGI |
--without-http_grpc_module | gRPC 代理 |
--without-http_memcached_module | memcached |
--without-http_limit_conn_module | 限连接 |
--without-http_limit_req_module | 限请求 |
--without-http_autoindex_module | 目录浏览 |
--without-http_geo_module | geo 变量 |
--without-http_map_module | map 指令 |
--without-http_split_clients_module | 分流 |
--without-http_referer_module | referer 检查 |
--without-http_upstream_*_module | 各类 upstream 负载算法 |
Nginx 可选 HTTP 模块(--with- 开启,默认关)
| 参数 | 说明 | 公网网关建议 |
|---|---|---|
--with-http_ssl_module | HTTPS | ✅ 必开 |
--with-http_v2_module | HTTP/2 | ✅ 必开 |
--with-http_v3_module | HTTP/3 | 可选(需 OpenSSL 支持) |
--with-http_realip_module | 取真实 IP | ✅ 必开 |
--with-http_stub_status_module | 基础状态页 | ✅ 必开 |
--with-http_gzip_static_module | 预压缩 .gz 直发 | ✅ 建议 |
--with-http_sub_module | 响应体替换 | ✅ 建议(脱敏) |
--with-http_auth_request_module | 子请求鉴权 | ✅ 网关建议 |
--with-threads | 线程池 | ✅ 建议 |
--with-file-aio | 异步文件 IO | ✅ 建议 |
--with-http_addition_module | 响应前后追加 | ❌ 少用 |
--with-http_xslt_module[=dynamic] | XML 转换 | ❌ |
--with-http_image_filter_module[=dynamic] | 图片处理 | ❌ |
--with-http_geoip_module[=dynamic] | GeoIP | ❌ 用 Lua 替代 |
--with-http_dav_module | WebDAV | ❌ 公网禁用 |
--with-http_flv_module | FLV 伪流 | ❌ |
--with-http_mp4_module | MP4 伪流 | ❌ |
--with-http_gunzip_module | 服务端 gunzip | ❌ |
--with-http_random_index_module | 随机首页 | ❌ |
--with-http_secure_link_module | 防盗链 | 可选 |
--with-http_degradation_module | 内存降级 | 可选 |
--with-http_slice_module | 大文件分片 | 可选(CDN 场景) |
--with-http_perl_module[=dynamic] | Perl 嵌入 | ❌ |
Stream(TCP/UDP)模块
| 参数 | 说明 | 公网网关 |
|---|---|---|
--with-stream[=dynamic] | 四层代理 | ✅ 建议 |
--with-stream_ssl_module | stream SSL | ✅ 建议 |
--with-stream_ssl_preread_module | SNI 预读路由 | ✅ 建议 |
--with-stream_realip_module | 取真实 IP | ✅ 建议 |
--with-stream_geoip_module[=dynamic] | GeoIP | ❌ |
--without-stream_limit_conn_module | 四层限连 | 可选关 |
--without-stream_map_module 等 | 同 HTTP 类 | 按需 |
Mail 模块(基本不用)
| 参数 | 说明 |
|---|---|
--with-mail[=dynamic] | POP3/IMAP/SMTP 代理 |
--with-mail_ssl_module | 邮件 SSL |
--without-mail_pop3_module 等 | 关闭子协议 |
网关场景一律不加
--with-mail。
OpenResty 专属参数(重点)
LuaJIT / Lua
| 参数 | 说明 |
|---|---|
--with-luajit | 编入内置 LuaJIT 2.1(默认开) |
--with-luajit=DIR | 用外部 LuaJIT |
--with-luajit-xcflags= | LuaJIT 编译参数 |
--with-luajit-ldflags= | LuaJIT 链接参数 |
--without-luajit-lua52 | 关 Lua 5.2 兼容扩展 |
--without-luajit-gc64 | 关 GC64(x86_64 默认开) |
--with-lua51 | 编入标准 Lua 5.1(不推荐) |
-jN | 并行编 LuaJIT |
OpenResty 自带 Lua/Nginx 模块(默认开,可 --without- 禁)
--without-http_lua_module--without-http_lua_upstream_module--without-stream_lua_module--without-ngx_devel_kit_module--without-http_echo_module--without-http_xss_module--without-http_coolkit_module--without-http_set_misc_module--without-http_form_input_module--without-http_encrypted_session_module--without-http_srcache_module--without-http_headers_more_module--without-http_array_var_module--without-http_memc_module--without-http_redis2_module--without-http_redis_module--without-http_rds_json_module/--without-http_rds_csv_module
OpenResty 自带 Lua 库(默认带,可 --without-lua_resty_* 禁)
--without-lua_cjson--without-lua_resty_core--without-lua_resty_redis--without-lua_resty_mysql--without-lua_resty_dns--without-lua_resty_lrucache--without-lua_resty_lock--without-lua_resty_limit_traffic--without-lua_resty_websocket等
可选额外模块
--with-http_iconv_module:编码转换--with-http_drizzle_module:直连 MySQL--with-http_postgres_module:直连 PG
公网网关「记录用」推荐基线
1 | ./configure \ |
显式关闭(缩小攻击面):
1 | --without-http_dav_module \ |
一句话记忆法
- 路径类:
--prefix系列 - 依赖类:
--with-pcre/openssl/zlib(或-devel系统包) - HTTP 必开:
ssl / v2 / realip / stub_status - HTTP 选开:
threads / file-aio / gzip_static / sub / auth_request - Stream 选开:
stream / stream_ssl / ssl_preread / realip - OpenResty 灵魂:
--with-luajit(SkyWalking/Prometheus Lua 都靠它) - 公网别开:
dav / xslt / image_filter / geoip / flv / mp4 / mail / perl
以上参数覆盖 ./configure --help 的 95% 以上内容,剩下少量平台相关(select/poll/kqueue)Linux 生产用不到,可不记。