文章目录
Tengine/Nginx 安装以及模块查看与扩展
Tengine 是淘宝开源的 Nginx 增强版,增加了负载均衡、动态模块、Sticky Session 等功能。本指南将从安装、模块查看到安全运维实践进行详细说明。
安装
安装依赖
bash
yum install -y gcc make wget tar pcre pcre-devel zlib zlib-devel openssl openssl-devel openssl-dev
说明:Tengine 需要 C 编译环境及一些系统库,缺少依赖会导致编译失败。
下载 Tengine
bash
cd /usr/local/src
wget http://tengine.taobao.org/download/tengine-3.1.0.tar.gz
tar zxvf tengine-3.1.0.tar.gz
cd tengine-3.1.0
编译 Tengine
bash
./configure --prefix=/usr/local/tengine
make -j$(nproc)
make install
说明 :根据需求选择要编译的模块,可以添加第三方模块的路径。
安装完成后,Tengine 默认路径在
/usr/local/tengine
。
配置环境变量
bash
echo 'export PATH=$PATH:/usr/local/tengine/sbin' >> /etc/profile
source /etc/profile
配置 nginx.conf
bash
cp /usr/local/tengine/conf/nginx.conf /usr/local/tengine/conf/nginx.conf.bak
vi /usr/local/tengine/conf/nginx.conf
conf
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 102400;
}
http {
include mime.types;
default_type application/octet-stream;
log_format json '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
# 上传文件大小限制
client_max_body_size 100M;
include /usr/local/tengine/conf.d/*.conf;
}
检查启动
bash
mkdir -p /usr/local/tengine/conf.d
# 测试配置是否正确
nginx -t
# 查看模块是否生效
nginx -V
示例输出:
text
[root@test ~]# nginx -t
nginx: the configuration file /usr/local/tengine/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/tengine/conf/nginx.conf test is successful
[root@test ~]# nginx -V
Tengine version: Tengine/3.1.0
nginx version: nginx/1.24.0
built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC)
built with OpenSSL 3.2.2 4 Jun 2024
TLS SNI support enabled
configure arguments: --prefix=/usr/local/tengine/
[root@test ~]#
-
nginx -V
:显示编译信息和配置参数。 -
解析:
Tengine version: Tengine/3.1.0
→ 你安装的 Tengine 版本是 3.1.0。nginx version: nginx/1.24.0
→ 内核 Nginx 版本是 1.24.0。built by gcc 11.5.0
→ 用 GCC 11.5 编译。built with OpenSSL 3.2.2
→ 支持最新的 OpenSSL 3。TLS SNI support enabled
→ 支持多域名 SSL(Server Name Indication)。configure arguments: --prefix=/usr/local/tengine/
→ 安装目录是/usr/local/tengine/
,只编译了最基础模块,没有像http_ssl_module
或sticky_session
这种可选模块。
配置系统启动项
/usr/lib/systemd/system/tengine.service
bash
[Unit]
Description=Nginx Web Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/tengine/sbin/nginx
ExecReload=/usr/local/tengine/sbin/nginx -s reload
ExecStop=/usr/local/tengine/sbin/nginx -s stop
KillMode=process
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
启动以及停止
bash
# 查看tengine服务状态
systemctl status tengine
# 启动tengine服务
systemctl start tengine
# 停止tengine服务
systemctl stop tengine
# 重启tengine服务
systemctl restart tengine
# 开机自启tengine服务
systemctl enable tengine
# 关闭开机自启tengine服务
systemctl disable tengine
# 查看tengine日志
tail -f /usr/local/tengine/logs/error.log
# 查看tengine访问日志
tail -f /usr/local/tengine/logs/access.log
生产环境谨慎操作哦
加载模块
查看已编译模块
Tengine/Nginx 的模块是在编译时通过 ./configure
指定的。查看当前安装的模块,可以使用以下命令:
bash
nginx -V
示例输出:
text
Tengine version: Tengine/3.1.0
nginx version: nginx/1.24.0
built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC)
built with OpenSSL 3.2.2 4 Jun 2024
TLS SNI support enabled
configure arguments: --prefix=/usr/local/tengine/
configure arguments: --prefix=/usr/local/tengine/
→ 安装目录是/usr/local/tengine/
,只编译了最基础模块,没有像http_ssl_module
或sticky_session
这种可选模块。
常见模块及作用
模块名称 | 功能简介 |
---|---|
http_stub_status_module | 监控 Nginx 状态 |
http_v2_module | 支持 HTTP/2 |
http_realip_module | 获取客户端真实 IP |
http_ssl_module | 支持 HTTPS |
http_lua_module | Lua 脚本扩展 |
http_upstream_sticky_module | 支持 Sticky Session |
注意 :如果看到
unknown directive "sticky"
,说明 Sticky Session 模块未编译,需要重新编译或加载动态模块。
重新编译 Tengine
增加模块需要重新编译 Tengine,在 ./configure
阶段指定需要的模块。例如:
bash
cd tengine-3.1.0
./configure --prefix=/usr/local/tengine --add-module=./modules/ngx_http_upstream_session_sticky_module
make -j$(nproc)
make install
然后验证模块是否生效:
bash
nginx -V
示例输出:
text
Tengine version: Tengine/3.1.0
nginx version: nginx/1.24.0
built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC)
built with OpenSSL 3.2.2 4 Jun 2024
TLS SNI support enabled
configure arguments: --prefix=/usr/local/tengine --add-module=./modules/ngx_http_upstream_session_sticky_module
测试配置并平滑重启
bash
# 测试配置是否正确
nginx -t
# 平滑重启
nginx -s reload
平滑重启不会中断正在处理的请求,保证业务可用。
常见问题排查
问题 | 原因 | 解决方法 |
---|---|---|
unknown directive "sticky" | 模块未编译或未加载 | 编译模块或使用 load_module 动态加载 |
make 报错 | 缺少依赖或版本不匹配 | 安装依赖或使用对应模块源码 |
nginx -s reload 报错 | 配置语法错误 | 使用 nginx -t 检查配置 |
小知识点
ngx_http_upstream_session_sticky_module 模块用于实现 Sticky Session 功能,确保同一用户的请求被路由到同一后端服务器。常见配置示例如下:
conf
upstream backend {
session_sticky cookie=route domain=.example.com mode=insert; # 设置 Sticky Session
server 10.0.0.1:8080;
server 10.0.0.2:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
}
}
小结
本文讲解了:
- Tengine 安装与依赖
- 已编译模块查看方法
- 平滑重启及业务安全注意事项
- 常见问题排查
通过以上步骤,您可以灵活管理 Tengine 模块,保证业务稳定运行,同时支持高级功能扩展,如 Sticky Session、Lua 扩展等。