目录
为已通过源码编译安装的 Nginx 增加 echo模块,核心在于在不破坏现有配置的前提下,通过重新编译将新模块集成进去。本文在LNMP脚本安装的nginx基础上,增加echo模块,下面是每个步骤的具体操作和指令。
一、准备工作与编译安装
-
检查现有Nginx配置
首先,你需要知道当前Nginx的编译参数,这是后续编译的基础。使用以下命令查看:
/usr/local/nginx/sbin/nginx -V请将路径
/usr/local/nginx/sbin/nginx替换为你的实际路径。记录下输出中configure arguments:后面的所有参数。root@localhost mnt]# nginx -V nginx version: nginx/1.26.0 built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC) built with OpenSSL 1.1.1w 11 Sep 2023 TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_realip_module --with-openssl=/mnt/lnmp2.2/src/openssl-1.1.1w --with-openssl-opt='enable-weak-ssl-ciphers' --with-pcre=/mnt/lnmp2.2/src/pcre-8.45 --with-pcre-jit -
下载Nginx源码与echo模块
确保你下载的Nginx源码版本与当前正在运行的版本完全一致。
-
Nginx源码 : 访问 Nginx官方下载页获取对应版本的源码包并解压。
-
echo模块 : 从官方仓库下载,例如使用
wget:wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz tar zxvf v0.61.tar.gz
-
-
重新编译Nginx
进入Nginx源码目录,使用之前的参数并添加echo模块进行配置。
-
静态模块:模块会直接编译进Nginx核心。
./configure [你之前复制的所有参数] --add-module=/path/to/your/echo-nginx-module-0.61本文案例:
#进入src目录 cd /mnt/lnmp2.2/src #解压nginx、openssl、pcre压缩包 tar xf nginx-1.26.0.tar.gz tar xf openssl-1.1.1w.tar.gz tar xf pcre-8.45.tar.bz2 #配置 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_realip_module --with-openssl=/mnt/lnmp2.2/src/openssl-1.1.1w --with-openssl-opt='enable-weak-ssl-ciphers' --with-pcre=/mnt/lnmp2.2/src/pcre-8.45 --with-pcre-jit --add-module=/mnt/lnmp2.2/src/echo-nginx-module-0.61 -
配置完成后,执行编译:
make
⚠️ 重要提示:
1)此步骤只执行
make,不要执行make install,否则会覆盖现有安装;2)需要解压openssl、pcre压缩包。
-
二、替换与升级
-
备份并替换二进制文件
编译完成后,在源码目录的
objs文件夹里会生成新的nginx可执行文件。-
备份:首先备份正在运行的Nginx旧二进制文件,这是重要的安全措施。
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak -
替换:将新编译的二进制文件复制到Nginx的安装目录。
cp -f objs/nginx /usr/local/nginx/sbin/nginx
-
-
平滑升级(可选)
如果希望在不中断服务的情况下升级,可以在Nginx源码目录下执行:
make upgrade
三、验证与配置使用
-
验证模块安装
-
再次执行
nginx -V,查看输出中是否包含了你添加的echo模块路径(如--add-module=...)。 -
检查配置文件语法:
nginx -t
-
-
配置echo模块
echo模块安装成功后,你可以在Nginx的配置文件中使用它。这里是一些常见用法示例:
-
基础输出:
server{ listen 888; server_name 10.0.16.7; location /test_basic { default_type text/plain; echo "\n"; echo "Request URI: $request_uri"; echo "uri: $uri"; echo "request_filename: $request_filename"; echo "args: $args"; echo "Remote Address: $remote_addr"; echo "User Agent: $http_user_agent"; echo "Document Root: $document_root"; echo "request_method: $request_method"; echo "http_user_agent: $http_user_agent"; echo "http_referer: $http_referer"; echo "server_name: $server_name"; echo "server_port: $server_port"; echo "host: $host"; echo "status: $status"; echo "body_bytes_sent: $body_bytes_sent"; } -
高级功能(延迟/重复输出):
location /test_advanced { default_type text/plain; echo "开始..."; echo_sleep 1; # 延迟1秒 echo_duplicate 3 "重复三次 "; # 重复输出 echo "结束。"; }
-
四、操作要点总结
为已安装的Nginx添加第三方模块,关键在于获取原有配置参数 、正确重新编译 (只make不make install)以及妥善备份 和替换 。完成后来命令行试试 curl http://你的服务器地址/test_basic,应该就能看到 echo 模块输出的信息了。