构建包含 nginx_upstream_check_module 的轻量级 Nginx 1.21.5 Docker 镜像
以下是一个基于 Alpine Linux 的轻量级 Docker 镜像构建方案,包含 Nginx 1.21.5 和第三方模块 nginx_upstream_check_module。
Dockerfile
dockerfile # 使用 Alpine Linux 作为基础镜像 FROM alpine:3.14 # 安装必要的构建工具和依赖 RUN apk add --no-cache --virtual .build-deps \ gcc \ libc-dev \ make \ openssl-dev \ pcre-dev \ zlib-dev \ linux-headers \ curl \ gnupg \ wget \ && apk add --no-cache pcre \ && rm -rf /var/cache/apk/* # 下载并解压 Nginx 源码 ENV NGINX_VERSION 1.21.5 RUN wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz \ && tar -xzf nginx-${NGINX_VERSION}.tar.gz \ && rm nginx-${NGINX_VERSION}.tar.gz # 下载 nginx_upstream_check_module RUN git clone https://github.com/yaoweibin/nginx_upstream_check_module.git \ && cd nginx_upstream_check_module \ && git checkout v0.3.0 # 兼容 Nginx 1.21.5 的版本 # 配置并编译 Nginx RUN cd nginx-${NGINX_VERSION} \ && ./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-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-http_slice_module \ --with-threads \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_ssl_preread_module \ --with-compat \ --with-pcre \ --with-pcre-jit \ --with-ld-opt="-Wl,-rpath,/usr/lib" \ --add-module=../nginx_upstream_check_module \ && make -j$(getconf _NPROCESSORS_ONLN) \ && make install # 清理构建依赖 RUN apk del .build-deps \ && rm -rf /nginx-${NGINX_VERSION} \ && rm -rf /nginx_upstream_check_module # 创建 nginx 用户和组 RUN addgroup -S nginx \ && adduser -S -G nginx nginx # 创建必要的目录 RUN mkdir -p /var/cache/nginx \ && chown -R nginx:nginx /var/cache/nginx # 复制默认配置文件 COPY nginx.conf /etc/nginx/nginx.conf # 暴露端口 EXPOSE 80 443 # 停止信号 STOPSIGNAL SIGQUIT # 启动命令 CMD ["nginx", "-g", "daemon off;"]
默认 nginx.conf 配置文件
nginx user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; 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; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; upstream backend { server 127.0.0.1:8080; check interval=3000 rise=2 fall=3 timeout=1000 type=http; check_http_send "HEAD /health HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } server { listen 80; server_name localhost; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } } }
构建和使用说明
- 将上述内容保存为
Dockerfile
和nginx.conf
在同一目录下 - 构建镜像:
bash docker build -t nginx-with-check-module .
- 运行容器:
bash docker run -d -p 80:80 -p 443:443 --name my-nginx nginx-with-check-module
注意事项
- 这个构建使用了 Alpine Linux 作为基础镜像,保持了轻量级特性。
- nginx_upstream_check_module 是一个第三方模块,用于提供上游服务器的健康检查功能。
- 默认配置中已经包含了一个简单的健康检查配置示例。
- 如果需要更复杂的配置,可以修改
nginx.conf
文件或挂载自定义配置到容器中。 - 对于生产环境,建议使用更具体的 Nginx 和模块版本标签,而不是 master/latest。
- 如果需要其他 Nginx 模块,可以在
./configure
命令中添加相应的参数。