编程 宝塔在 Tengine-Nginx 已编译情况下增加模块

2024-11-18 23:09:44 +0800 CST views 1320

Tengine 是由淘宝网开发并开源的一个基于 Nginx 的 Web 服务器,提供了许多 Nginx 不具备的增强功能和优化模块。因此,Tengine 在许多高性能和高并发的 Web 服务场景中得到了广泛使用。

在浏览 Tengine 官方网站(http://tengine.taobao.org/)时,你可能会发现一些非常有用的功能模块,例如:

  • ngx_http_concat_module 模块:类似于 Apache 的 mod_concat 模块,可以将多个 CSS 或 JavaScript 文件合并在一个响应中,减少 HTTP 请求数,提升网页加载速度。
  • ngx_http_trim_filter_module 模块:用于删除 HTML、内嵌的 JavaScript 和 CSS 中的注释及重复的空白符,从而减小文件体积,进一步优化网页性能。

这些功能对于优化网站性能非常有帮助。然而,在尝试直接修改 Nginx 配置以使用这些功能时,你可能会发现宝塔默认安装的 Tengine 并未编译这些模块。

添加模块的步骤

1. 查看已编译的模块列表

使用以下命令查看当前已编译的模块列表:

nginx -V

将输出的信息复制保存,后续步骤会用到。我的输出信息如下:

./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module

2. 检查 Nginx 模块文件

Nginx 的模块文件通常位于 /www/server/nginx/src/modules 目录下。进入该目录并搜索相关模块文件:

cd /www/server/nginx/src/modules

使用 Ctrl+F 搜索模块名的中间部分,例如 concat_,以确认模块文件是否存在。如果已存在,则无需重新下载模块文件。

3. 修改 Nginx 的 configure 配置

进入 Nginx 的源码目录:

cd /www/server/nginx/src

执行修改,将之前保存的 ./configure 输出信息后面添加需要增加的模块路径:

./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/www/server/nginx/src/modules/ngx_http_trim_filter_module --add-module=/www/server/nginx/src/modules/ngx_http_concat_module

4. 编译 Nginx

src 目录下执行编译:

make

注意:不要执行 make install,否则会覆盖安装当前的 Nginx。

5. 替换 Nginx 二进制文件

执行以下命令备份并替换 Nginx 二进制文件:

rm -f /www/server/nginx/sbin/nginx.old
mv /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx.old
cp objs/nginx /www/server/nginx/sbin/nginx
make upgrade

编译升级完成后,使用 nginx -V 再次查看版本信息,确认新模块已加载成功。

6. 使用模块

trim 模块为例,修改单个网站的 Nginx 配置:

location / {
    trim on;
    trim_js on;
    trim_css on;
}

保存配置并重启 Nginx,刷新网站查看效果。

总结

以上步骤展示了在宝塔面板管理的 Tengine-Nginx 已编译情况下,如何手动添加新模块。如果你不是使用宝塔安装的 Nginx,可以按照相同的思路操作,关键是找到 Nginx 的 configure 文件并修改相关设置项,然后重新编译即可。

复制全文 生成海报 Web服务器 性能优化 开源软件 宝塔

推荐文章

如何在 Linux 系统上安装字体
2025-02-27 09:23:03 +0800 CST
JavaScript 策略模式
2024-11-19 07:34:29 +0800 CST
浅谈CSRF攻击
2024-11-18 09:45:14 +0800 CST
如何实现生产环境代码加密
2024-11-18 14:19:35 +0800 CST
如何配置获取微信支付参数
2024-11-19 08:10:41 +0800 CST
404错误页面的HTML代码
2024-11-19 06:55:51 +0800 CST
详解 Nginx 的 `sub_filter` 指令
2024-11-19 02:09:49 +0800 CST
js迭代器
2024-11-19 07:49:47 +0800 CST
Go 单元测试
2024-11-18 19:21:56 +0800 CST
使用 Vue3 和 Axios 实现 CRUD 操作
2024-11-19 01:57:50 +0800 CST
Golang Select 的使用及基本实现
2024-11-18 13:48:21 +0800 CST
在JavaScript中实现队列
2024-11-19 01:38:36 +0800 CST
Vue3中如何实现响应式数据?
2024-11-18 10:15:48 +0800 CST
使用Python实现邮件自动化
2024-11-18 20:18:14 +0800 CST
虚拟DOM渲染器的内部机制
2024-11-19 06:49:23 +0800 CST
程序员茄子在线接单