Tengine/Nginx 安装以及模块查看与扩展

文章目录


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_modulesticky_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_modulesticky_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 扩展等。


相关推荐
ping某12 小时前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树882 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠2 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质2 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工2 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智2 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_2 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉3 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦3 天前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
java_cj3 天前
深入kube-apiserver认证机制:从Bearer Token到mTLS的完整认证链解析
linux·运维·服务器·云原生·容器·kubernetes