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

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

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服务器 性能优化 开源软件 宝塔

推荐文章

Vue3中的Slots有哪些变化?
2024-11-18 16:34:49 +0800 CST
filecmp,一个Python中非常有用的库
2024-11-19 03:23:11 +0800 CST
Gin 框架的中间件 代码压缩
2024-11-19 08:23:48 +0800 CST
H5抖音商城小黄车购物系统
2024-11-19 08:04:29 +0800 CST
使用 Go Embed
2024-11-19 02:54:20 +0800 CST
pycm:一个强大的混淆矩阵库
2024-11-18 16:17:54 +0800 CST
Vue中的异步更新是如何实现的?
2024-11-18 19:24:29 +0800 CST
gin整合go-assets进行打包模版文件
2024-11-18 09:48:51 +0800 CST
实现微信回调多域名的方法
2024-11-18 09:45:18 +0800 CST
FcDesigner:低代码表单设计平台
2024-11-19 03:50:18 +0800 CST
一个简单的打字机效果的实现
2024-11-19 04:47:27 +0800 CST
避免 Go 语言中的接口污染
2024-11-19 05:20:53 +0800 CST
Go的父子类的简单使用
2024-11-18 14:56:32 +0800 CST
mysql int bigint 自增索引范围
2024-11-18 07:29:12 +0800 CST
程序员茄子在线接单