Alpine-Nginx-定制nginx

nginx-Dockerfile

(base) root@ubuntu:~/test-nginx# cat nginx-Dockerfile 
# 第一阶段: 构建阶段
FROM alpine:latest AS build

# 安装构建工具和依赖库
RUN apk update && apk add --no-cache \
    build-base \
    pcre-dev \
    zlib-dev \
    gd-dev \
    geoip-dev \
    openssl-dev \
    curl \
    git

# 下载并解压 Nginx 源码
RUN curl -LO http://nginx.org/download/nginx-1.27.1.tar.gz \
    && tar -zxvf nginx-1.27.1.tar.gz \
    && rm nginx-1.27.1.tar.gz

# BR算法
RUN git clone https://github.com/google/ngx_brotli.git \
    && cd ngx_brotli \
    && git submodule update --init \
    && cd ..

# 编译 Nginx
RUN cd nginx-1.27.1 && ./configure --prefix=/usr/local/nginx \
    --with-http_dav_module \
    --with-http_addition_module \
    --with-http_realip_module \
    --with-http_sub_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_gunzip_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-http_secure_link_module \
    --with-http_image_filter_module \
    --with-http_random_index_module \
    --with-http_auth_request_module \
    --with-stream \
    --with-stream_realip_module \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-stream_geoip_module=dynamic \
    --with-threads \
    --with-pcre \
    --with-pcre-jit \
    --with-compat \
    --with-select_module \
    --with-poll_module \
    --add-module=../ngx_brotli \
    && make \
    && make install \
    && apk del build-base

# 第二阶段: 生产阶段
FROM alpine:latest

# 安装必要的运行时依赖
RUN apk add --no-cache \
    pcre \
    zlib \
    gd \
    geoip \
    openssl \
    bash \
    iproute2 \
    procps \
    curl

# 复制编译后的 Nginx 和配置
COPY --from=build /usr/local/nginx /usr/local/nginx
COPY nginx.conf /usr/local/nginx/conf/nginx.conf

RUN ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx && mkdir /var/log/nginx -p

# 设置工作目录
WORKDIR /usr/local/nginx

# 暴露端口 80 和 443
EXPOSE 80 443

# 启动 Nginx
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

示例

Last updated