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


相关推荐
有谁看见我的剑了?8 小时前
kvm 虚拟机如何安装 qemu-guest-agent
运维
東雪蓮☆8 小时前
深入掌握sed:Linux文本处理的流式编辑器利器
linux·运维·编辑器
一张假钞8 小时前
Ubuntu 24.04 中 nvm 安装 Node 权限问题解决
linux·运维·ubuntu
耐达讯通信技术8 小时前
“乾坤大挪移”:耐达讯自动化RS485转Profinet解锁HMI新乾坤
运维·网络·物联网·自动化·信息与通信
耐达讯通信技术9 小时前
惊爆!耐达讯自动化RS485转Profinet,电机连接的“逆天神器”?
运维·网络·人工智能·科技·网络协议·自动化
三坛海会大神5559 小时前
sed——Stream Editor流编辑器
linux·运维·编辑器
zzz100669 小时前
Web 与 Nginx 网站服务:从基础到实践
运维·前端·nginx
二进制coder10 小时前
Linux内存管理章节五:Linux物理内存管理的核心:伙伴系统深入分析
linux·运维·服务器
广药门徒10 小时前
正点原子LINUX imx6ull开发板的nfs传输配置(传输慢,失败等问题)
linux·运维·服务器