欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 构建nginx1.26.1轻量级Docker镜像添加第三方模块nginx_upstream_check_module

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

2025/4/16 13:10:36 来源:https://blog.csdn.net/jianghuchuang/article/details/143906287  浏览:    关键词:构建nginx1.26.1轻量级Docker镜像添加第三方模块nginx_upstream_check_module

文章目录

  • 1.构建自定义nginx镜像原因
  • 2.准备构建文件
  • 3.构建镜像
  • 4.验证第三方模块是否加载成功

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 builderARG NGINX_VERSION=1.26.1
ENV DEBIAND_FRONTEND=noninteractiveRUN 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 unzipRUN 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_moduleRUN 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 \--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=/tmp/nginx/client_temp \--http-proxy-temp-path=/tmp/nginx/proxy_temp \--http-fastcgi-temp-path=/tmp/nginx/fastcgi_temp \--http-uwsgi-temp-path=/tmp/nginx/uwsgi_temp \--http-scgi-temp-path=/tmp/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-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 \--with-cc-opt="-g -O2 -ffile-prefix-map=/data/builder/debuild/nginx-$NGINX_VERSION/debian/debuild-base/nginx-$NGINX_VERSION=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC" \--with-ld-opt="-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie" \--add-module=/nginx_upstream_check_module \&& make \&& make installFROM ${BASE_IMAGE}RUN addgroup --gid 999 nginx \&& adduser \--uid 999 \--gid 999 \--no-create-home \--shell /bin/bash \--disabled-password \--gecos "" \--quiet \nginxRUN mkdir -p /usr/share/nginxCOPY --from=builder --chown=nginx:nginx /etc/nginx /etc/nginx
COPY --from=builder /usr/sbin/nginx /usr/sbin/nginx
COPY --from=builder --chown=nginx:nginx /etc/nginx/html /usr/share/nginx/htmlCOPY --chown=nginx:nginx nginx.conf /etc/nginx/
COPY --chown=nginx:nginx default.conf /etc/nginx/conf.d/RUN mkdir -p /tmp/nginx \&& chown nginx:nginx /tmp/nginxRUN mkdir -p /etc/nginx/conf.d \&& chown nginx:nginx /etc/nginx/conf.dRUN mkdir -p /var/log/nginx \&& chown nginx:nginx /var/log/nginxRUN touch /var/log/nginx/access.log \&& touch /var/log/nginx/error.log \&& ln -sf /dev/stdout /var/log/nginx/access.log \&& ln -sf /dev/stderr /var/log/nginx/error.logRUN chown -R nginx:nginx /var/log/nginxRUN rm -f /etc/nginx/*.default \&& rm -f /etc/nginx/*-utf \&& rm -f /etc/nginx/*-win \&& rm -rf /etc/nginx/htmlENTRYPOINT /usr/sbin/nginx -g 'daemon off;'

nginx.conf

user  nginx;
worker_processes  auto;error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;include /etc/nginx/conf.d/*.conf;
}

default.conf

server {listen       80;server_name  localhost;#access_log  /var/log/nginx/host.access.log  main;location / {root   /usr/share/nginx/html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}
}

3.构建镜像

docker build -t das-nginx:1.26.1 .

:构建过程需要联网下载文件、依赖等。故而可以选择能够连公网的服务器去构建镜像。倘若构建过程发现apt-get update更新失败,那么 检查服务器是否具备网络、或者更换为国内的安装源、又或者换成一台公有云服务器进行尝试。
:我这边使用本地虚拟机去构建镜像,虚拟机能连公网,但构建镜像过程中,apt-get update总是更新失败。这个过程各种怀疑,各种尝试都不行。最后找了一台 公有云服务器进行镜像构建,出乎意料的是,apt-get update马上就行执行成功了,也不需要换国内源。

4.验证第三方模块是否加载成功

启动容器

docker run -itd --name nginx -p 3333:3333 -p 80:80 -v /tmp/default.conf:/etc/nginx/conf.d/default.conf das-nginx:1.26.1

/tmp/default.conf (文件内容如下)

upstream myservers {server 172.168.110.85:9003;server 172.168.110.86:9003;# 每5s(interval) 通过http方式(type)请求url地址(check_http_send)检查一次,如果请求超时(timeout=1000)或者响应状态码不在指定范围(check_http_expect_alive),则认为是失败。# 一旦连续5次(fall)失败,则认为服务不可用,从可用服务列表中剔除。如果连续成功2次(rise),那么将服务重新添加回可用服务列表check interval=5000 rise=2 fall=5 timeout=1000 type=http;# 这里的/healthy配置的是你后台服务 用于健康检查的uri,需要结合实际而定。check_http_send "HEAD /healthy  HTTP/1.0\r\n\r\n";   check_http_expect_alive http_2xx http_3xx;
}server {listen 3333;server_name localhost;location /status {# check_status是第三方插件自带的功能,用于展示服务列表的健康检查结果界面check_status;access_log   off;}location / {proxy_pass http://myservers/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}server {listen       80;server_name  localhost;#access_log  /var/log/nginx/host.access.log  main;location / {root   /usr/share/nginx/html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}
}[

访问nginx首页:
在这里插入图片描述


访问健康检查界面:
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词