1.构建自定义nginx镜像原因
- docker hub仓库里的nginx官方镜像太大了,足足188MB
- 不能重新引入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