温馨提示×

怎样给debian加nginx模块

小樊
46
2026-09-30 16:33:43
栏目: 智能运维

在 Debian 上给 Nginx 添加模块,一般有两种方式:

  1. 动态模块(推荐,Nginx 1.9.11+ 支持)
  2. 静态编译(重新编译 Nginx)

下面分别说明。


一、先确认 Nginx 版本和类型

nginx -v
nginx -V
  • 如果是 nginx version: nginx/1.xx.x(官方或 Debian 源)
  • 看 --modules-path 和 --with-compat 是否存在

二、方式一:使用动态模块(最简单)

1️⃣ 安装动态模块(以官方 Nginx 为例)

如果你用的是 Nginx 官方仓库(推荐):

sudo apt install nginx-module-<模块名>

常见模块:

  • nginx-module-image-filter
  • nginx-module-geoip
  • nginx-module-njs
  • nginx-module-perl

示例:

sudo apt install nginx-module-njs

2️⃣ 加载模块

编辑配置文件:

sudo nano /etc/nginx/nginx.conf

在 最顶部 添加:

load_module modules/ngx_http_js_module.so;

3️⃣ 测试并重载

sudo nginx -t
sudo systemctl reload nginx

✅ 完成


三、方式二:从源码编译添加模块(静态)

适用于:

  • Debian 自带 Nginx
  • 需要第三方模块(如 ngx_brotli、lua-nginx-module)

1️⃣ 安装编译依赖

sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev \
zlib1g zlib1g-dev libssl-dev dpkg-dev

2️⃣ 下载 Nginx 源码

apt source nginx

或官方:

wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -xzf nginx-1.25.3.tar.gz
cd nginx-1.25.3

3️⃣ 获取原编译参数

nginx -V

复制 --configure arguments 内容

4️⃣ 添加模块编译

示例(添加 ngx_brotli):

git clone https://github.com/google/ngx_brotli.git
./configure <原参数> --add-module=../ngx_brotli
make

⚠️ 不要 make install

5️⃣ 替换二进制文件

sudo systemctl stop nginx
sudo cp objs/nginx /usr/sbin/nginx
sudo systemctl start nginx

四、常见模块示例

模块 说明
ngx_brotli Brotli 压缩
lua-nginx-module Lua 脚本
ngx_http_geoip2_module IP 地理位置
headers-more-nginx-module 修改响应头

五、建议

✅ 优先用动态模块 ✅ 用官方 Nginx 源(nginx.org) ❌ 不建议直接覆盖 Debian 系统包


如果你告诉我:

  • Debian 版本(11 / 12)
  • Nginx 来源(apt / 官方)
  • 想加什么模块

我可以给你精确命令。

0 踩