docker - 在 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 下的标题中隐藏服务器?
解决方案
我想我在这里有一个更简单的解决方案: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
推荐阅读
- javascript - 5 列背景DIV填充空白
- php - 在重力形式列表列中添加下拉和日期选择器字段
- maven - com.sun.javafx.fxml.FXMLLoaderHelper 类无法访问 com.sun.javafx.util.Utils 类
- wordpress - WordPress add_action 错误?
- wpf - 在 ListView 控件中设置所选项目的背景颜色
- java - 货币转换器代码需要帮助。有解决办法吗?
- php - Cakephp 脚本从命令行运行良好,但不能从 cron
- python - 编写自定义字符串格式化程序
- c# - excel 发现无法读取的内容,对于通过 StreamWriter 存储的数据 file = new StreamWriter(@"Pee.xlsx", true)
- c# - 部署的 ASP.Net 应用程序不返回数据