构建包含 nginx_upstream_check_module 的轻量级 Nginx 1.21.5 Docker 镜像

简介: 测试

构建包含 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;
        }
    }
}

构建和使用说明

  1. 将上述内容保存为 Dockerfilenginx.conf 在同一目录下
  2. 构建镜像:
bash
docker build -t nginx-with-check-module .
  1. 运行容器:
bash
docker run -d -p 80:80 -p 443:443 --name my-nginx nginx-with-check-module

注意事项

  1. 这个构建使用了 Alpine Linux 作为基础镜像,保持了轻量级特性。
  2. nginx_upstream_check_module 是一个第三方模块,用于提供上游服务器的健康检查功能。
  3. 默认配置中已经包含了一个简单的健康检查配置示例。
  4. 如果需要更复杂的配置,可以修改 nginx.conf 文件或挂载自定义配置到容器中。
  5. 对于生产环境,建议使用更具体的 Nginx 和模块版本标签,而不是 master/latest。
  6. 如果需要其他 Nginx 模块,可以在 ./configure 命令中添加相应的参数。
目录
相关文章
|
2月前
|
人工智能 前端开发 Docker
从本地到云端:用 Docker Compose 与 Offload 构建可扩展 AI 智能体
在 AI 智能体开发中,开发者常面临本地调试与云端部署的矛盾。本文介绍如何通过 Docker Compose 与 Docker Offload 解决这一难题,实现从本地快速迭代到云端高效扩容的全流程。内容涵盖多服务协同、容器化配置、GPU 支持及实战案例,助你构建高效、一致的 AI 智能体开发环境。
268 1
从本地到云端:用 Docker Compose 与 Offload 构建可扩展 AI 智能体
|
2月前
|
JavaScript Docker 容器
使用Docker多阶段构建优化镜像大小
使用Docker多阶段构建优化镜像大小
279 100
|
2月前
|
缓存 安全 Linux
优化Docker镜像大小的多阶段构建实践
优化Docker镜像大小的多阶段构建实践
243 99
|
2月前
|
缓存 Docker 容器
优化Docker镜像大小的五个实用技巧
优化Docker镜像大小的五个实用技巧
230 98
|
23天前
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
209 5
|
2月前
|
Java Docker 容器
使用Docker多阶段构建优化镜像大小
使用Docker多阶段构建优化镜像大小
118 8
|
应用服务中间件 Linux nginx
|
2月前
|
编解码 应用服务中间件 Linux
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
208 1
下一篇
开通oss服务