如何在已有的nginx镜像中集成headers-more-nginx-module
第一步在外网“一键编译”出 .so 文件1. 创建并进入一个临时工作目录mkdir -p nginx-build cd nginx-build2.全自动编译headers_more_filter_module.sodocker run --rm -it --platform linux/arm64 -v $PWD:/output centos:7 /bin/bash -c \ sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo \ sed -i s/^#.*baseurlhttp/baseurlhttp/g /etc/yum.repos.d/*.repo \ sed -i s/^mirrorlisthttp/#mirrorlisthttp/g /etc/yum.repos.d/*.repo \ yum clean all yum makecache \ yum install -y gcc make pcre-devel zlib-devel openssl-devel wget git tar \ cd /tmp \ wget http://nginx.org/download/nginx-1.23.2.tar.gz \ tar -zxvf nginx-1.23.2.tar.gz \ wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.34.tar.gz -O headers-more.tar.gz \ tar -zxvf headers-more.tar.gz \ cd nginx-1.23.2 \ ./configure --with-compat --add-dynamic-module../headers-more-nginx-module-0.34 \ make modules \ cp objs/ngx_http_headers_more_filter_module.so /output/ \ 运行结束后你在当前的 nginx-build 目录下就会看到一个新鲜出炉的 ngx_http_headers_more_filter_module.so 文件。3.修改nginx.conf# 最顶层加载外部动态模块 load_module /etc/nginx/modules/ngx_http_headers_more_filter_module.so; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; # 这里就可以全局愉快地使用 headers-more 模块的指令了比如隐藏服务器版本 more_clear_headers Server; server { listen 80; server_name localhost; location / { # 这里的路径对应你 Dockerfile 里的路径 root /app/nginx/html/dist; index index.html index.htm; } # 解决前端 Vue/React 路由刷新 404 的经典问题 location router { rewrite ^.*$ /index.html last; } } }4.dockerfile重新构建# 1. 指定基础镜像 FROM asianux7.5_nginx-release_arm:1.23.2 # 2. 设置工作目录 WORKDIR /app/nginx # 3. 创建存放动态模块的专用目录 RUN mkdir -p /etc/nginx/modules # 4. 拷贝编译好的动态模块文件到镜像内 COPY ngx_http_headers_more_filter_module.so /etc/nginx/modules/ # 5. 覆盖 Nginx 配置文件 # 注意根据 Asianux 镜像打包习惯配置文件可能在 conf/ 目录下也可能在 /etc/nginx/ 下。 # 为了稳妥我们可以直接覆盖这两个常见位置或根据你实际情况保留一个。 COPY nginx.conf conf/nginx.conf COPY nginx.conf /etc/nginx/nginx.conf # 6. 拷贝前端代码到镜像内 COPY dist/ html/dist # 7. 暴露端口注释掉的根据原镜像决定通常原镜像已经有了 # EXPOSE 805.构建与运行构建业务镜像docker build -t my-frontend-nginx:v1 .运行并测试docker run -d --name my-web -p 8080:80 my-frontend-nginx:v1