瀏覽代碼

优化nginx配置

rainwer 6 月之前
父節點
當前提交
1c6faba61a
共有 2 個文件被更改,包括 44 次插入6 次删除
  1. 36 5
      Dockerfile
  2. 8 1
      nginx.conf

+ 36 - 5
Dockerfile

@@ -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

+ 8 - 1
nginx.conf

@@ -1,4 +1,4 @@
-worker_processes  1;
+worker_processes  auto;
 
 events {
     worker_connections  1024;
@@ -12,6 +12,13 @@ http {
     sendfile        on;
     keepalive_timeout  65;
     client_max_body_size 20m;
+    # 启用动态压缩
+    gzip on;
+    gzip_types text/plain text/css application/json application/javascript text/xml;
+    # 启用预压缩文件支持
+    gzip_static on;
+    # 允许解压(兼容不支持gzip的客户端)
+    gunzip on;
 
     server { 
 	listen 8079;