如何编译加载Nginx动态模块?

Nginx

1 前言

一个问题,一篇文章,一出故事。
笔者最近学习编译Nginx发现Nginx跟Apache一样,也支持动态模块,于是整理此文。

2 最佳实践

2.1 实践环境

如果你没有nginx环境,可参阅如下章节部署,

如何编译安装nginx?

2.2 编译前准备

2.2.1 准备Nginx安装包

cd ~/
wget https://nginx.org/download/nginx-1.22.1.tar.gz
tar -xf nginx-1.22.1.tar.gz

2.2.2 准备模块的编译安装包

mkdir ~/nginx-1.22.1/modules
cd ~/nginx-1.22.1/modules
wget https://github.com/perusio/nginx-hello-world-module/archive/refs/heads/master.zip -O nginx-hello-world-module.zip
unzip nginx-hello-world-module.zip

2.3 编译并部署模块

2.3.1 执行模块预编译

cd ~/nginx-1.22.1/
./configure --with-compat --add-dynamic-module=./modules/nginx-hello-world-module-master

注:以上需要模块本身支持动态链接

2.3.2 编译模块

cd ~/nginx-1.22.1/
make modules

2.3.3 部署模块

mkdir -p /etc/nginx/modules
cd ~/nginx-1.22.1/
cp objs/ngx_http_hello_world_module.so /etc/nginx/modules/

2.4 加载动态模块并测试

2.4.1 加入载入配置

sed -i '1i\load_module modules/ngx_http_hello_world_module.so;' /etc/nginx/nginx.conf

然后,你需要使用如下命令确认配置无语法错误,

nginx -t

2.4.2 增加测试配置

vim /etc/nginx/nginx.conf

加入如下配置,

server {
    listen 80;

    #...
    location / {
         #...
         hello_world;
    }
    #...
}

2.4.3 重载配置使服务生效

systemctl reload nginx.service

2.4.4 测试

http://10.168.0.80
可见如下显示,

参阅文档
================

nginx动态模块加载
——————
https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/?_ga=2.44134692.1415740220.1678322711-1427168807.1678322711==

nginx模块加载文档
—————
https://nginx.org/en/docs/ngx_core_module.html#load_module

nginx编译文档
—————–
https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/

nginx编译参数
——————-
https://nginx.org/en/docs/configure.html

nginx-hello-world-module
———————–
https://github.com/perusio/nginx-hello-world-module

没有评论

发表回复

Nginx
如何解决https请求不安全http页被阻止问题?

1 前言 一个问题,一篇文章,一出故事。 笔者最近代理公司应用,发现https的页面有请求不安全的h …

Nginx
如何Nginx代理上游的子项目或文件夹?

1 前言 一个问题,一篇文章,一出故事。 笔者接到任务需要把Tomcat的其中一个应用号使用Ngin …

Nginx
如何安装部署RHEL 9 Nignx?

1 前言 一个问题,一篇文章,一出故事。 笔者需要基于RHEL 9部署Nginx环境,于是整理此文。 …