在Debian系统上,Nginx模块的安装主要分为两种方式:通过包管理器安装官方预编译模块(简单快捷,推荐常规需求)和从源码编译安装自定义模块(灵活可控,适合需要特定功能的场景)。
sudo apt update
sudo apt install nginx
http_subs_filter
为例):libnginx-mod-<module-name>
形式提供,可通过以下命令安装:sudo apt install libnginx-mod-http-subs-filter
其他常见模块(如http_echo
、http_brotli_filter
)同理,只需替换模块名称即可。nginx -V 2>&1 | grep --color=auto -o with-http_\w+_module
若模块已加载,输出中会显示对应模块名称(如with-http_subs_filter_module
)。若需要安装第三方模块(如echo-nginx-module
)或自定义模块,需通过源码编译实现。
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev
这些依赖用于编译Nginx源码及模块。
从Nginx官方网站下载稳定版源码(替换<version>
为实际版本号,如1.25.3
):
wget http://nginx.org/download/nginx-<version>.tar.gz
tar -zxvf nginx-<version>.tar.gz
cd nginx-<version>
http_ssl_module
为例):./configure --with-http_ssl_module
echo-nginx-module
为例,需提前下载模块源码并替换<module-path>
):./configure --add-module=<module-path>
可同时添加多个模块,例如:./configure --with-http_ssl_module --add-module=/path/to/echo-nginx-module
make # 编译源码(耗时较长,取决于系统性能)
sudo make install # 安装到默认路径(通常为/usr/local/nginx)
/etc/nginx/nginx.conf
或/usr/local/nginx/conf/nginx.conf
),在http
、server
或location
块中添加模块对应的配置指令。echo-nginx-module
后,可配置以下内容测试:http {
server {
listen 80;
server_name localhost;
location /echo {
echo "Hello, Nginx!";
}
}
}
sudo systemctl restart nginx # 重启Nginx服务
sudo systemctl status nginx # 检查服务状态(确保为"active (running)")
http://<server-ip>/echo
,若看到Hello, Nginx!
响应,则说明模块加载成功。nginx -V 2>&1 | grep --color=auto -o with-http_\w+_module
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
)。sudo nginx -s reload
命令应用配置变更。