源码编译安装的Nginx增加echo模块过程详解

目录

一、准备工作与编译安装

二、替换与升级

三、验证与配置使用

四、操作要点总结


为已通过源码编译安装的 Nginx 增加 echo模块,核心在于在不破坏现有配置的前提下,通过重新编译将新模块集成进去。本文在LNMP脚本安装的nginx基础上,增加echo模块,下面是每个步骤的具体操作和指令。

一、准备工作与编译安装

  1. 检查现有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   
  2. 下载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
  3. 重新编译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压缩包。

二、替换与升级

  1. 备份并替换二进制文件

    编译完成后,在源码目录的 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
  2. 平滑升级(可选)

    如果希望在不中断服务的情况下升级,可以在Nginx源码目录下执行:

    复制代码
    make upgrade

三、验证与配置使用

  1. 验证模块安装

    • 再次执行 nginx -V,查看输出中是否包含了你添加的echo模块路径(如 --add-module=...)。

    • 检查配置文件语法:

      复制代码
      nginx -t
  2. 配置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添加第三方模块,关键在于获取原有配置参数正确重新编译makemake install)以及妥善备份替换 。完成后来命令行试试 curl http://你的服务器地址/test_basic,应该就能看到 echo 模块输出的信息了。

相关推荐
卷到起飞的数分1 小时前
5.MyBatis持久(dao)层框架
java·数据库·mybatis
浪漫血液&1 小时前
事务ACID(四个核心特性)
数据库
胖咕噜的稞达鸭2 小时前
进程状态,孤儿进程僵尸进程,Linux真实调度算法,进程切换
linux·运维·算法
hen3y2 小时前
清理 Git 代码库大文件历史记录
运维·git
岚天start2 小时前
Nginx内置变量详解
运维·nginx
wanhengidc2 小时前
跨境电商为什么依赖于云手机
运维·服务器·游戏·智能手机·云计算
王道长服务器 | 亚马逊云2 小时前
直播站怎么做到“低延迟不掉线”?AWS + 拳头链路的实战组合
服务器·数据库·搜索引擎·云计算·aws
翼龙云_cloud2 小时前
亚马逊云渠道商:aws安全组没有加ip用ip访问会有什么问题?
运维·tcp/ip·安全·云计算·aws
e***0962 小时前
【MySQL】MySQL库的操作
android·数据库·mysql