当前位置: 首页 > news >正文

Nginx运维之路(Docker多段构建新版本并增加第三方模块)

喜大普奔,前两天发现Nginx竟然自带支持了ACME功能,让我很想测试一下,但是发现手头没有资源让我测试,忽然我想到可以用docker来构建nginx然后测试ACME功能,在这个过程中发现原来官方Nginx镜像并没有集成ACME插件,只有少的可怜的几个插件测试不了acme,这怎么能忍,所以我就想是否能够自行编译第三方插件并加入到新Nginx镜像中,那么话不多说开干!喜欢折腾的朋友也可以加群一起讨论哦!如果无法看到图片可以私信哈!

1.创建Dockerfile

root@wolfan-NUC9V7QNX:~/Docker_Build_image# cat Dockerfile
# 使用 nginx:1.29.1 作为构建基础镜像
FROM nginx:1.29.1 AS build# 安装构建依赖
RUN apt-get update && apt-get install -y --no-install-recommends \build-essential \curl \git \libpcre3-dev \libssl-dev \zlib1g-dev \ca-certificates \libxml2-dev \libxslt1-dev \pkg-config \openssl \build-essential \libtool \libssl-dev \libpcre2-dev \zlib1g-dev \pkg-config \wget \clang \libclang-dev \&& rm -rf /var/lib/apt/lists/*# 安装 Rust 和 Cargo
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain stable -y# 设置环境变量
ENV PATH="/root/.cargo/bin:$PATH"# 验证 Rust 和 Cargo 是否安装成功
RUN echo $PATH && ls /root/.cargo/bin && cargo --version# 下载 Nginx 源代码
RUN curl -fSL https://nginx.org/download/nginx-1.29.1.tar.gz -o nginx.tar.gz \&& tar -xzvf nginx.tar.gz \&& cd nginx-1.29.1RUN git clone https://github.com/openresty/echo-nginx-module.git /tmp/echo-nginx-module \&& git clone https://github.com/vozlt/nginx-module-vts.git /tmp/nginx-module-vts \&& git clone https://github.com/openresty/rds-json-nginx-module.git /tmp/rds-json-nginx-module \&& git clone https://github.com/openresty/memc-nginx-module.git /tmp/memc-nginx-module \&& git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git /tmp/ngx_http_substitutions_filter_module \&& git clone https://github.com/openresty/redis2-nginx-module.git /tmp/redis2-nginx-module \&& git clone https://github.com/openresty/headers-more-nginx-module.git /tmp/headers-more-nginx-module \&& git clone https://github.com/FRiCKLE/ngx_cache_purge.git /tmp/ngx_cache_purge \&& git clone https://github.com/nginx/nginx-acme.git /tmp/nginx-acme# 配置和编译 Nginx
RUN cd nginx-1.29.1 \&& ./configure --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-http_v2_module \--with-http_v3_module \--with-mail \--with-mail_ssl_module \--with-stream \--with-stream_realip_module \--with-stream_ssl_module \--with-stream_ssl_preread_module \--add-dynamic-module=/tmp/echo-nginx-module \--add-dynamic-module=/tmp/redis2-nginx-module \--add-dynamic-module=/tmp/nginx-module-vts \--add-dynamic-module=/tmp/rds-json-nginx-module \--add-dynamic-module=/tmp/memc-nginx-module \--add-dynamic-module=/tmp/ngx_http_substitutions_filter_module \--add-dynamic-module=/tmp/headers-more-nginx-module \--add-dynamic-module=/tmp/ngx_cache_purge \--add-dynamic-module=/tmp/nginx-acme \&& make -j$(nproc) modules \&& mkdir -pv /usr/lib/nginx/modules \&& cp objs/*.so /usr/lib/nginx/modules/ \&& cd .. \&& rm -rf nginx-1.29.1 nginx.tar.gz /tmp/*# 第二阶段:最小化的运行环境
FROM nginx:1.29.1# 复制编译好的模块
COPY --from=build /usr/lib/nginx/modules/*.so /usr/lib/nginx/modules/# 创建 Nginx 的默认目录
RUN mkdir -p /etc/nginx/conf.d /var/log/nginx# 复制自定义配置文件(可选)
#COPY nginx.conf /etc/nginx/nginx.conf# 暴露端口
EXPOSE 80 443# 启动 Nginx
CMD ["nginx", "-g", "daemon off;"]


构建命令

root@wolfan-NUC9V7QNX:~/Docker_Build_image# docker build -t wolf-nginx-mulit:1.29.1 .
[+] Building 1.1s (15/15) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 4.20kB 0.0s
=> [internal] load metadata for docker.io/library/nginx:1.29.1 0.9s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [build 1/9] FROM docker.io/library/nginx:1.29.1@sha256:d5f28ef21aabddd098f3dbc21fe5b7a7d7a184720bc07da0b 0.0s
=> CACHED [build 2/9] RUN apt-get update && apt-get install -y --no-install-recommends build-essential 0.0s
=> CACHED [build 3/9] RUN wget https://github.com/LuaJIT/LuaJIT/archive/refs/tags/v2.1.0-beta3.tar.gz && 0.0s
=> CACHED [build 4/9] RUN ls /usr/local/include/luajit-2.1 0.0s
=> CACHED [build 5/9] RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-to 0.0s
=> CACHED [build 6/9] RUN echo /root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bi 0.0s
=> CACHED [build 7/9] RUN curl -fSL https://nginx.org/download/nginx-1.29.1.tar.gz -o nginx.tar.gz && tar 0.0s
=> CACHED [build 8/9] RUN git clone https://github.com/openresty/echo-nginx-module.git /tmp/echo-nginx-modu 0.0s
=> CACHED [build 9/9] RUN cd nginx-1.29.1 && ./configure --with-compat --with-file-aio --with-threads 0.0s
=> CACHED [stage-1 2/3] COPY --from=build /usr/lib/nginx/modules/*.so /usr/lib/nginx/modules/ 0.0s
=> CACHED [stage-1 3/3] RUN mkdir -p /etc/nginx/conf.d /var/log/nginx 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:eebda8668546569b99b80b701783c788de6be06cceaddfa2a44a88a454c1cdd3 0.0s
=> => naming to docker.io/library/wolf-nginx-mulit:1.29.1 0.0s

What's Next?

  1. 1. Sign in to your Docker account → docker login
  2. 2. View a summary of image vulnerabilities and recommendations → docker scout quickview

2.为什么要多段构建

多段构建就是为了保持最小的镜像体积(下面就是多段构建和没有多段构建的区别)

root@wolfan-NUC9V7QNX:~/Docker_Build_image# docker images |grep 1.29
wolf-nginx-mulit                                                 1.29.1                         eebda8668546   7 hours ago     206MB
wolf-nginx-nomulit                                               1.29.1                         f5fe69dfb6f3   7 hours ago     2.42GB

3.启动一个nginx看是否有了加载的模块

# 启动一个容器并查看ID
root@wolfan-NUC9V7QNX:~/Docker_Build_image# docker run -it -d --name wolf-nginx-mulit wolf-nginx-mulit:1.29.1
f49189c25fe5b80f135df3396098c6019216f89c7d5524d012c68d78522cd777
root@wolfan-NUC9V7QNX:~/Docker_Build_image# docker ps |grep wolf-nginx-mulit
f49189c25fe5   wolf-nginx-mulit:1.29.1                                                 "/docker-entrypoint.…"    10 seconds ago   Up 9 seconds             80/tcp, 443/tcp                                                                                wolf-nginx-mulit
# 进入已经启动的容器
root@wolfan-NUC9V7QNX:~/Docker_Build_image# docker exec -it f49189c25fe5 /bin/bash
# 可以看到所有模块
root@f49189c25fe5:/# ls /usr/lib/nginx/modules/
ngx_http_acme_module.so			ngx_http_image_filter_module-debug.so  ngx_http_rds_json_filter_module.so	ngx_http_xslt_filter_module.so
ngx_http_echo_module.so			ngx_http_image_filter_module.so        ngx_http_redis2_module.so		ngx_stream_geoip_module-debug.so
ngx_http_geoip_module-debug.so		ngx_http_js_module-debug.so	       ngx_http_subs_filter_module.so		ngx_stream_geoip_module.so
ngx_http_geoip_module.so		ngx_http_js_module.so		       ngx_http_vhost_traffic_status_module.so	ngx_stream_js_module-debug.so
ngx_http_headers_more_filter_module.so	ngx_http_memc_module.so		       ngx_http_xslt_filter_module-debug.so	ngx_stream_js_module.so

因为折腾一天流水线改造,所以功能上我还没有验证,应该没什么问题,待我边整理流水线边把acme这个功能输出给大家!

image-20250908171958500


文章转载自:

http://Moz2uryK.zqsnj.cn
http://vkOApVO6.zqsnj.cn
http://jnzUFdZm.zqsnj.cn
http://H3Wy4t71.zqsnj.cn
http://pxCPekmS.zqsnj.cn
http://pRlRflW0.zqsnj.cn
http://7I3JQgLg.zqsnj.cn
http://4QncesuD.zqsnj.cn
http://tbuyvy3J.zqsnj.cn
http://EJRwdZPP.zqsnj.cn
http://yiiGeF8G.zqsnj.cn
http://3JagJdlT.zqsnj.cn
http://OJnafL59.zqsnj.cn
http://vVKxOUrU.zqsnj.cn
http://S5Ljoo1r.zqsnj.cn
http://8UJuoYL4.zqsnj.cn
http://K0qHo7bH.zqsnj.cn
http://9iBT3Q43.zqsnj.cn
http://FOyO5IrA.zqsnj.cn
http://DIQs3BO3.zqsnj.cn
http://BRrt4BY0.zqsnj.cn
http://UXRCVsYD.zqsnj.cn
http://FYJhqMzC.zqsnj.cn
http://tBDcz4gT.zqsnj.cn
http://1ovligpH.zqsnj.cn
http://BEYtPr7L.zqsnj.cn
http://UUB6lRoM.zqsnj.cn
http://LF8o1VSJ.zqsnj.cn
http://6zkgdfCt.zqsnj.cn
http://WQ0Y1pl8.zqsnj.cn
http://www.dtcms.com/a/375437.html

相关文章:

  • 构造方法与代替代码构造方法的注解
  • 开源模型应用落地-基于KTO的Qwen3-4B意图理解精准对齐实践(二十一)
  • 微信小程序加速计开发指南
  • Python中ORM的理解
  • Spark Streaming 实时流处理入门
  • 单片机学习笔记.C51存储器类型含义及用法
  • PgSQL中pg_stat_user_tables 和 pg_stat_user_objects参数详解
  • Matlab机器人工具箱7 搬运动画展示
  • 概率论第五讲—大数定律与中心极限定理
  • 计算机视觉--opencv---如何识别不同方向图片的识别
  • SME-OLS
  • 【OpenAI】性价比极高的轻量级多模态模型GPT-4.1-mini介绍 + API KEY的使用教程!
  • 机器学习-聚类
  • MyBatis基础到高级实践:全方位指南(中)
  • CLR的GC机制
  • 《投资-48》- 股市有哪些相互矛盾的说法?
  • 传统商业积分的普遍困境与 RWA 的破局可能
  • 稳定币法律困局:游走在创新与监管的钢丝绳上
  • 第三方区块链应用测评:【多签钱包合约安全评估_阈值签名机制与私钥存储安全性测试】
  • 【服务器】将本地项目部署到服务器
  • 串的模式匹配(朴素算法和KMP算法以及KMP的改进算法)
  • 基于LLM的月全食时空建模与智能预测:当古老天文遇见深度学习
  • php redis 中文API文档手册
  • 哪些危化品企业的岗位需要持有安全员证?
  • Linux指令基础
  • Modbus 速查与实战笔记(功能码、帧结构、坑点)
  • Label Smoothing Cross Entropy(标签平滑交叉熵) 是什么
  • 亮相cippe 成都石油展,陀螺定向短节带来高精度无磁导向方案
  • Debian 操作系统全面介绍
  • Java全栈开发工程师面试实战:从基础到微服务的深度解析