构建包含 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 命令中添加相应的参数。
目录
相关文章
|
1月前
|
缓存 Shell 网络安全
将应用程序打包成Docker镜像时可能遇到哪些问题?
将应用程序打包成Docker镜像时可能遇到哪些问题?
189 77
|
1月前
|
Shell 应用服务中间件 nginx
docker 镜像的部分常用命令
docker镜像常用命令
75 16
|
1月前
|
关系型数据库 MySQL Docker
|
28天前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
390 87
|
25天前
|
负载均衡 应用服务中间件 nginx
Nginx配置与命令
Nginx 是一款高性能的 HTTP 和反向代理服务器,其配置文件灵活且功能强大。本文介绍了 Nginx 配置的基础结构和常用指令,包括全局块、Events 块、HTTP 块及 Server 块的配置方法,以及静态资源服务、反向代理、负载均衡、HTTPS 和 URL 重写等功能实现。此外,还提供了常用的 Nginx 命令操作,如启动、停止、重载配置和日志管理等,帮助用户高效管理和优化服务器性能。
|
7月前
|
缓存 应用服务中间件 网络安全
Nginx中配置HTTP2协议的方法
Nginx中配置HTTP2协议的方法
415 7
|
8月前
|
应用服务中间件 BI nginx
Nginx的location配置详解
【10月更文挑战第16天】Nginx的location配置详解
|
3月前
|
应用服务中间件 nginx
Nginx进程配置指令详解
Nginx进程配置指令主要包括:`worker_processes`设置工作进程数;`worker_cpu_affinity`绑定CPU核心;`worker_rlimit_nofile`设置最大文件描述符数量;`worker_priority`设置进程优先级;`worker_connections`设置最大连接数;`daemon`控制守护进程模式;`master_process`启用主进程模式;`pid`设置PID文件路径;`user`指定用户和组;`error_log`配置错误日志。这些指令在`nginx.conf`中配置,用于优化和控制Nginx的运行行为。
165 10
|
5月前
|
存储 应用服务中间件 Linux
nginx配置证书和私钥进行SSL通信验证
nginx配置证书和私钥进行SSL通信验证
212 4
|
7月前
|
负载均衡 监控 应用服务中间件
配置Nginx反向代理时如何指定后端服务器的权重?
配置Nginx反向代理时如何指定后端服务器的权重?
368 61