你会用Nginx的第三方模块吗?

在这里插入图片描述

你好,我是赵兴晨,97年文科程序员。

你使用过Nginx的第三方模块吗?今天咱们来聊聊Nginx的第三方模块。

在深入了解Nginx的高性能与灵活性的过程中,我们不可避免地会接触到第三方模块。

这些模块是对Nginx原生功能的有力扩展,它们通常需要再编译安装Nginx时通过 --add-module=PATH 参数指定第三方模块的路径来集成。

一些模块由企业开发者针对特定业务需求定制开发,而另一些则是由开源社区的爱好者们精心打造并分享至GItHUb的成果。

第三方模块的集成与编译

以开源的echo模块为例,该模块位于 https://github.com/openresty/echo-nginx-module。以下是一段示例配置,展示如何在Nginx配置文件中使用echo模块:

location /main {
    index index.html;
    default_type text/html;
    echo "hello world,main-->";
    echo $remote_addr;
    echo_reset_timer;  # 重置计时器开始时间为当前时间
    echo_location /sub1;
    echo_location /sub2;
    echo "took $echo_timer_elapsed sec for total.";
}

location /sub1 {
    echo_sleep 1;
    echo sub1;
}

location /sub2 {
    echo_sleep 1;
    echo sub2;
}

然而,在尝试配置时,我遇到了一个错误

nginx: [emerg] unknown directive "echo_reset_timer" in /apps/nginx/conf/conf.d/pc.conf:86
nginx: configuration file /apps/nginx/conf/nginx.conf test failed

这个错误表明Nginx配置文件中包含一个无法识别的指令echo_reset_timer。

Nginx在解析配置文件时无法识别这个指令,因此报错,并且测试配置文件失败。

需要安装echo模块去解决。以下是安装步骤:

1、确保git已安装,并且克隆echo模块的源代码到本地

2、在Nginx源码目录下使用./configure命令来配置Nginx编译选项,并指定echo模块的路径。

3、重新编译并替换Nginx二进制文件

配置命令如下:

# 安装 git
[root@localhost ~]# yum install -y git

# 下载echo模块源码
[root@localhost ~]# git clone https://github.com/openresty/echo-nginx-module.git

# 预编译 添加echo模块
[root@localhost ~]# cd nginx-1.20.1
[root@localhost nginx-1.20.1]# ./configure --prefix=/usr/nginx --add-module=/root/echo-nginx-module

# 编译Nginx
[root@localhost nginx-1.20.1]# make

# 编译好的Nginx在 objs目录,覆盖掉之前的Nginx即可
[root@localhost nginx-1.20.1]# mv objs/nginx /usr/nginx/sbin/

# 查看版本和模块
[root@localhost nginx-1.20.1]# /usr/nginx/sbin/nginx -V
nginx version: nginx/1.20.1
configure arguments: --prefix=/usr/nginx --add-module=/root/echo-nginx-module

测试与验证:

重新编译之后,对Nginx进行语法检测并重启Nginx服务

[root@localhost conf]# ../sbin/nginx -t
nginx: the configuration file /usr/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/nginx/conf/nginx.conf test is successful

[root@localhost conf]# systemctl restart nginx

通过简单的curl命令,验证配置Nginx第三方echo模块是否生效

[root@localhost conf]# curl 10.0.0.10/main
hello world,main-->
10.0.0.10
sub1
sub2
took 2.008 sec for total.

OK,今天的分享就到这,通过这次实践,我们不仅学会了如何集成和使用Nginx的第三方模块,还加深了对NGINX配置和模块化的理解,感谢你的陪伴,希望这些分享在未来的某一天对你有所启发。让我们期待下一次的相遇,继续在技术的道路上共同成长。

相关推荐

  1. 几个好Thinkphp扩展

    2024-06-05 20:32:03       23 阅读
  2. python之模块

    2024-06-05 20:32:03       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-05 20:32:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-05 20:32:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-05 20:32:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-05 20:32:03       20 阅读

热门阅读

  1. h5相机功能

    2024-06-05 20:32:03       7 阅读
  2. 机器人编程课有什么东西:探索编程的奇妙世界

    2024-06-05 20:32:03       10 阅读
  3. 如何使用 Apache 和 Nginx 创建临时和永久重定向

    2024-06-05 20:32:03       9 阅读
  4. webpack怎么配置单页面或者多页面项目?

    2024-06-05 20:32:03       8 阅读
  5. Spark中广播的使用

    2024-06-05 20:32:03       8 阅读