|
@@ -1,8 +1,39 @@
|
|
|
-# 基础镜像
|
|
|
-#FROM nginx:1.22
|
|
|
-FROM nginx
|
|
|
+# 使用官方Nginx镜像作为基础
|
|
|
+FROM nginx:1.27.3 AS builder
|
|
|
|
|
|
-# 复制 nginx 配置文件
|
|
|
-COPY ./nginx.conf /etc/nginx/nginx.conf
|
|
|
+# 安装编译工具
|
|
|
+RUN apk add --no-cache \
|
|
|
+ build-base \
|
|
|
+ linux-headers \
|
|
|
+ pcre-dev \
|
|
|
+ zlib-dev
|
|
|
+
|
|
|
+# 下载Nginx源码(与基础镜像版本一致)
|
|
|
+RUN wget http://nginx.org/download/nginx-1.27.3.tar.gz && \
|
|
|
+ tar -zxvf nginx-1.27.3.tar.gz && \
|
|
|
+ rm nginx-1.27.3.tar.gz
|
|
|
+
|
|
|
+# 编译Nginx并启用所需模块
|
|
|
+WORKDIR /nginx-1.27.3
|
|
|
+RUN ./configure \
|
|
|
+ --prefix=/etc/nginx \
|
|
|
+ --sbin-path=/usr/sbin/nginx \
|
|
|
+ --modules-path=/usr/lib/nginx/modules \
|
|
|
+ --with-http_gzip_module \
|
|
|
+ --with-http_gzip_static_module \
|
|
|
+ --with-http_gunzip_module \
|
|
|
+ --with-http_ssl_module && \
|
|
|
+ make && \
|
|
|
+ make install
|
|
|
|
|
|
+# 第二阶段:构建最终镜像
|
|
|
+FROM nginx:1.27.3
|
|
|
|
|
|
+# 从builder阶段复制编译好的二进制文件
|
|
|
+COPY --from=builder /usr/sbin/nginx /usr/sbin/nginx
|
|
|
+
|
|
|
+# 验证模块
|
|
|
+RUN nginx -V 2>&1 | grep -E 'gzip|gunzip'
|
|
|
+
|
|
|
+# 复制 nginx 配置文件
|
|
|
+COPY ./nginx.conf /etc/nginx/nginx.conf
|