首页 > 解决方案 > 在 Alpine Linux 下编辑/隐藏 Nginx Server 标头

问题描述

当我curl --head用来测试我的网站时,它会返回服务器信息。

我按照本教程隐藏了 nginx 服务器标头。但是当我运行命令时yum install nginx-module-security-headers ,它会返回yum: not found

我也试过apk add nginx-module-security-headers了,显示包丢失。

我已将其用作nginx:1.17.6-alpine我的基本 docker 映像。有谁知道如何从这个 Alpine 下的标题中隐藏服务器?

标签: dockernginxalpine

解决方案


我想我在这里有一个更简单的解决方案:https ://gist.github.com/hermanbanken/96f0ff298c162a522ddbba44cad31081 。非常感谢 Github 上的 hermanbanken 分享这个要点。

这个想法是使用 nginx alpine 映像创建一个多阶段构建,作为编译模块的基础。这变成了以下内容Dockerfile

ARG VERSION=alpine
FROM nginx:${VERSION} as builder

ENV MORE_HEADERS_VERSION=0.33
ENV MORE_HEADERS_GITREPO=openresty/headers-more-nginx-module

# Download sources
RUN wget "http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz" -O nginx.tar.gz && \
    wget "https://github.com/${MORE_HEADERS_GITREPO}/archive/v${MORE_HEADERS_VERSION}.tar.gz" -O extra_module.tar.gz

# For latest build deps, see https://github.com/nginxinc/docker-nginx/blob/master/mainline/alpine/Dockerfile
RUN  apk add --no-cache --virtual .build-deps \
    gcc \
    libc-dev \
    make \
    openssl-dev \
    pcre-dev \
    zlib-dev \
    linux-headers \
    libxslt-dev \
    gd-dev \
    geoip-dev \
    perl-dev \
    libedit-dev \
    mercurial \
    bash \
    alpine-sdk \
    findutils

SHELL ["/bin/ash", "-eo", "pipefail", "-c"]

RUN rm -rf /usr/src/nginx /usr/src/extra_module && mkdir -p /usr/src/nginx /usr/src/extra_module && \
    tar -zxC /usr/src/nginx -f nginx.tar.gz && \
    tar -xzC /usr/src/extra_module -f extra_module.tar.gz

WORKDIR /usr/src/nginx/nginx-${NGINX_VERSION}

# Reuse same cli arguments as the nginx:alpine image used to build
RUN CONFARGS=$(nginx -V 2>&1 | sed -n -e 's/^.*arguments: //p') && \
    sh -c "./configure --with-compat $CONFARGS --add-dynamic-module=/usr/src/extra_module/*" && make modules


# Production container starts here
FROM nginx:${VERSION}

COPY --from=builder /usr/src/nginx/nginx-${NGINX_VERSION}/objs/*_module.so /etc/nginx/modules/

.... skipped inserting config files and stuff ...

# Validate the config
RUN nginx -t

推荐阅读