一、软件包获取
官网地址:https://openresty.org/en/
下载源码包:https://openresty.org/en/download.html
二、部署流程
# 解压软件包
tar -xf openresty-1.27.1.2.tar.gz
# 预编译
./configure \
--prefix=/usr/local/openresty \
--with-http_ssl_module \
--with-http_v2_module \
--with-openssl=/root/openresty/openssl-1.1.1w/ # 可以不指定,编译报错再指定
# 编译和安装
gmake && gmake install
# 编译报错处理
# 报错信息示例:
# objs/addon/src/ngx_http_lua_ssl_certby.o: In function `ngx_http_lua_ffi_ssl_client_random':
# /usr/local/openresty/build/nginx-1.27.1/../ngx_lua-0.10.28/src/ngx_http_lua_ssl_certby.c:1651: undefined reference to `SSL_get_client_random'
# objs/addon/src/ngx_stream_lua_ssl_certby.o: In function `ngx_stream_lua_ffi_ssl_client_random':
# /usr/local/openresty/build/nginx-1.27.1/../ngx_stream_lua-0.0.16/src/ngx_stream_lua_ssl_certby.c:1649: undefined reference to `SSL_get_client_random'
# collect2: error: ld returned 1 exit status
# 原因:OpenResty 从 1.19.3.1+ 开始要求 OpenSSL ≥ 1.1.1
## openssl 下载路径:
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
# 添加环境变量
echo 'export PATH=/usr/local/openresty/nginx/sbin:$PATH' >> ~/.bashrc
source ~/.bashrc
# 测试
nginx -v
# 使用 systemd 管理服务
cat > /etc/systemd/system/openresty.service << 'EOF'
[Unit]
Description=OpenResty Web Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
EOF
# 生效 systemd 配置
sudo systemctl daemon-reload
sudo systemctl enable openresty
# 服务管理命令
systemctl start openresty # 启动
systemctl stop openresty # 停止
systemctl reload openresty # 重载
systemctl restart openresty # 重启
三、系统优化以及配置
1. 文件描述符优化
# 编辑 /etc/security/limits.conf
* soft nofile 1000000
* hard nofile 1000000
# 创建 /etc/systemd/system/openresty.service.d/limit.conf(必须设置)
# 仅修改 /etc/security/limits.conf 无法生效于 systemd 管理的 OpenResty 进程
cat >/etc/systemd/system/openresty.service.d/limit.conf << 'EOF'
[Service]
LimitNOFILE=1048576
EOF
# 检查进程实际可用的描述符
cat /proc/$(pidof openresty | awk '{print $1}')/limits | grep "Max open files"
2. 内核参数优化
# 编辑内核参数配置文件(如 /etc/sysctl.conf 或 /etc/sysctl.d/99-openresty.conf)
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.ip_local_port_range = 1024 65000 # 增加本地端口范围
net.ipv4.tcp_fin_timeout = 15 # 减少FIN-WAIT-2状态的超时时间
fs.file-max = 2097152 # 最大文件数
# TCP缓冲区优化
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
# 内存管理
vm.swappiness = 10 # 减少交换分区的倾向
vm.vfs_cache_pressure = 50 # 控制文件系统缓存回收的速度
# 生效配置
sysctl -p
3. OpenResty 应用优化
(1)基础性能参数化配置
worker_processes auto; # NGINX自动设置worker进程数
worker_cpu_affinity auto; # 绑定CPU核心,减少CPU上下文切换
worker_rlimit_nofile 65535; # 每个 worker 进程可打开的最大文件描述符数
events {
worker_connections 16384; # 每个 worker 最大连接数
use epoll; # Linux 下最优 I/O 模型
multi_accept on; # 一次性接受所有连接
}
http {
keepalive_timeout 65; # 长连接超时时间
keepalive_requests 1000; # 每个连接最大请求数
tcp_nodelay on; # 禁用 Nagle 算法,降低延迟
sendfile on; # 高效文件传输
tcp_nopush on; # 结合 sendfile,优化数据包发送
client_body_buffer_size 128k; # 客户端请求体缓冲区大小
client_max_body_size 100m; # 最大请求体大小
server_tokens off; # 隐藏 Nginx/OpenResty 版本信息
}
(2)完整配置
worker_processes 6; # NGINX自动设置worker进程数
worker_cpu_affinity auto; # 绑定CPU核心,减少CPU上下文切换
worker_rlimit_nofile 1047552; # 每个 worker 进程可打开的最大文件描述符数
error_log /usr/local/openresty/nginx/logs/error.log warn;
events {
worker_connections 65536; # 每个 worker 最大连接数
use epoll; # Linux 下最优 I/O 模型
multi_accept on; # 一次性接受所有连接
}
http {
include mime.types;
default_type application/octet-stream;
# lua_shared_dict whitelist_cache 5m; #创建5M共享内存给lua脚本白名单缓存
lua_shared_dict config_cache 10m;
log_format json_access escape=json '{"timestamp":"$time_iso8601","client_ip":"$remote_addr","user":"$remote_user","method":"$request_method","uri":"$request_uri","host":"$host","protocol":"$server_protocol","status":$status,"bytes_sent":$body_bytes_sent,"referer":"$http_referer","user_agent":"$http_user_agent","request_time_sec":$request_time,"upstream_response_time":"$upstream_response_time","upstream_addr":"$upstream_addr"}';
access_log /usr/local/openresty/nginx/logs/access.log json_access;
# init_by_lua_file /usr/local/openresty/lua/json_logger.lua;
keepalive_timeout 65; # 长连接超时时间
keepalive_requests 1000; # 每个keep-alive连接处理最大请求数
tcp_nodelay on; # 禁用 Nagle 算法,降低延迟
sendfile on; # 高效文件传输
tcp_nopush on; # 结合 sendfile,优化数据包发送
client_body_buffer_size 1M; # 客户端请求体缓冲区大小
client_max_body_size 10G; # 最大请求体大小
server_tokens off; # 隐藏 Nginx/OpenResty 版本信息
upstream clusterA {
server 192.168.17.13 max_fails=5 fail_timeout=30s;
server 192.168.17.30 max_fails=5 fail_timeout=30s;
server 192.168.17.4 max_fails=5 fail_timeout=30s;
server 192.168.17.40 max_fails=5 fail_timeout=30s;
keepalive 128; # 保持 128 个空闲连接
}
upstream clusterB {
server 192.168.17.253 max_fails=5 fail_timeout=30s;
keepalive 128; # 保持 128 个空闲连接
}
server {
listen 80;
set $upstream "clusterA"; # 默认路由到 A 集群
access_by_lua_file /usr/local/openresty/lua/router.lua;
location / {
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding $http_accept_encoding;
# chunked 模式不与 content-length 冲突
proxy_buffering off; # 关闭响应缓冲,适用高并发
proxy_request_buffering off; # 关闭请求缓冲,使用高并发
proxy_cache off;
# 显式允许 chunked
chunked_transfer_encoding on;
proxy_connect_timeout 10;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_pass http://$upstream;
proxy_cookie_domain clusterA $host;
proxy_cookie_domain clusterB $host;
proxy_set_header Connection "";
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2; # 最多重试 2 次
proxy_next_upstream_timeout 10s; # 重试总超时时间
}
}
}
}
四、OpenResty 日志切割
使用 logrotate 工具实现日志切割和保留
1. 创建配置
sudo vim /etc/logrotate.d/openresty
2. 写入配置内容
/usr/local/openresty/nginx/logs/*.log {
daily
missingok
rotate 3
compress
delaycompress
notifempty
create 0644 nobody nobody
sharedscripts
postr
otate
# 通知 OpenResty 重新打开日志文件(避免继续写旧文件)
/usr/local/openresty/nginx/sbin/nginx -s reopen
endscript
}
3. 参数说明
- daily:每天轮转一次
- rotate 3:保留最近 3 个日志文件(即今天 + 前两天)
- compress:用 gzip 压缩旧日志(如 access.log.1.gz)
- delaycompress:延迟压缩,保留一个未压缩的 .1 文件(方便排查)
- create 0644 nobody nobody:创建新日志文件的权限和属主
- postrotate ... reopen:关键!让 Nginx 关闭旧文件句柄,写入新日志
4. 验证与测试
# 验证配置是否有效(仅模拟,不实际执行)
logrotate -d /etc/logrotate.d/openresty
# 强制立即轮转(用于测试)
logrotate -f /etc/logrotate.d/openresty
评论区