构建nginx1.26.1轻量级Docker镜像&添加第三方模块nginx_upstream_check_module

1.构建自定义nginx镜像原因

  1. docker hub仓库里的nginx官方镜像太大了,足足188MB
  2. 不能重新引入nginx内部模块 并且也 不能静态方式 添加nginx的第三方模块。因为此过程需要涉及对nginx源代码重新编译(confgiure)&编译(make)。而官方的nginx镜像里面没有nginx源代码,显然不支持重新配置&重编译。

扩展知识:引入nginx第三方模块有两种方式,即 静态模块方式、动态模块方式。


静态模块方式:需要对原有nginx可执行文件进行替换。即重新对nginx源代码进行配置(./configure --add-module= Module PATH)和编译(make); 编译成功之后,第三方模块动态被集成在nginx可执行文件里面,然后使用编译后新生成的nginx可执行文件覆盖掉原有nginx可执行文件即可。


动态模块方式:无需对原有nginx可执行文件进行替换。主要过程是先编译生成第三方模块xx.so文件,然后在nginx配置文件中,使用load_module 命令对xx.so第三方模块文件进行进入。但需要注意的是不是所有第三方模块都能生成.so文件,例如nginx_upstream_check_module。因此 就只能采用静态模块方式进行集成。
.
生成第三方模块.so文件 的流程为:在nginx源代码进行配置(./configure–add-dynamic-module= Module PATH)和编译(make); 编译成功之后,将会生成一个.so文件。


扩展知识:关于nginx_upstream_check_module


nginx_upstream_check_module是Nginx的一个第三方模块,它实现了Nginx的主动健康检查功能。该模块可以定期向后端服务器发送健康检查包(如HTTP请求),检测后端服务器的健康状况,并根据检测结果动态地调整负载均衡策略。当检测到后端服务器出现故障或宕机时,该模块会自动将该服务器从负载均衡池中移除,直到该服务器恢复正常工作,从而提高了后端服务器的可用性和稳定性。


2.准备构建文件

构建nginx,需要使用这个这三个文件。
在这里插入图片描述


Dockerfile

ARG BASE_IMAGE=ubuntu:jammy
FROM ${BASE_IMAGE} as builder

ARG NGINX_VERSION=1.26.1
ENV DEBIAND_FRONTEND=noninteractive

RUN apt-get update \
 && apt-get install -y wget build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev unzip

RUN wget https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz \
 && tar xvfz nginx-$NGINX_VERSION.tar.gz \
 && rm -f nginx-$NGINX_VERSION.tar.get \
 && mv nginx-$NGINX_VERSION nginx \
 && wget -O nginx_upstream_check_module-master.zip  https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/refs/heads/master \
 && unzip nginx_upstream_check_module-master.zip  \
 && mv nginx_upstream_check_module-master /nginx_upstream_check_module

RUN cd /nginx \
 && patch -p1 < /nginx_upstream_check_module/check_1.20.1+.patch \
 && ./configure \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --modules-path=/etc/nginx/modules \
    --conf-path=/etc/nginx/nginx.conf \
    --erro
### 构建带有 `nginx_upstream_check_module` 模块Nginx Docker 镜像 为了创建一个包含 `nginx_upstream_check_module` 的自定义 Nginx Docker 镜像,可以按照以下方法操作: #### 准备工作环境 首先,在本地环境中准备必要的文件和脚本。这包括编写用于构建镜像Dockerfile以及任何所需的shell脚本来简化模块安装过程。 #### 编写 Dockerfile 下面是一个简单的 Dockerfile 示例,它展示了如何从源码编译并集成 `nginx_upstream_check_module` 到官方 Nginx 容器中: ```dockerfile FROM nginx:latest as builder # 设置环境变量以避免交互提示 ENV DEBIAN_FRONTEND noninteractive RUN apt-get update &amp;&amp; \ apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev git wget unzip WORKDIR /usr/src # 下载指定版本的Nginx源码 (这里假设使用最新的稳定版) ARG NGINX_VERSION=1.21.6 RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz &amp;&amp; \ tar zxvf nginx-$NGINX_VERSION.tar.gz # 获取第三方模块源码 RUN wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master.zip &amp;&amp; \ unzip master.zip WORKDIR /usr/src/nginx-$NGINX_VERSION # 使用原始配置加上新添加模块选项重新编译Nginx RUN ./configure --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-mail \ --with-stream \ --add-dynamic-module=../nginx_upstream_check_module-master &amp;&amp; \ make modules # 创建最终运行时容器的基础层 FROM nginx:alpine COPY --from=builder /usr/src/nginx-$NGINX_VERSION/objs/ngx_http_upstream_check_module.so /etc/nginx/modules/ # 修改默认配置文件来加载动态模块和支持健康检查功能 RUN echo &#39;load_module modules/ngx_http_upstream_check_module.so;&#39; &gt;&gt; /etc/nginx/conf.d/default.conf # 将应用特定的配置复制到容器内 COPY conf.d/* /etc/nginx/conf.d/ ``` 此 Dockerfile 中的关键部分在于通过 `--add-dynamic-module` 参数指定了要加入的新模块路径,并且最后一步是确保新的 `.so` 文件被正确放置以便于后续由 Nginx 加载[^1]。 请注意上述命令中的具体路径可能依据实际环境有所变化;此外还需要根据实际情况调整所使用的依赖包列表及其版本号。 完成以上步骤之后就可以利用这个 Dockerfile 来构建自己的 Docker 镜像了。记得在执行 docker build 前先确认所有外部资源都可以正常访问并且能够成功下载下来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值