Edit / hide Nginx Server header under Alpine Linux Edit / hide Nginx Server header under Alpine Linux nginx nginx

Edit / hide Nginx Server header under Alpine Linux


I think I have an easier solution here: https://gist.github.com/hermanbanken/96f0ff298c162a522ddbba44cad31081. Big thanks to hermanbanken on Github for sharing this gist.

The idea is to create a multi stage build with the nginx alpine image to be a base for compiling the module. This turns into the following Dockerfile:

ARG VERSION=alpineFROM nginx:${VERSION} as builderENV MORE_HEADERS_VERSION=0.33ENV MORE_HEADERS_GITREPO=openresty/headers-more-nginx-module# Download sourcesRUN 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/DockerfileRUN  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 \    findutilsSHELL ["/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.gzWORKDIR /usr/src/nginx/nginx-${NGINX_VERSION}# Reuse same cli arguments as the nginx:alpine image used to buildRUN 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 hereFROM 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 configRUN nginx -t


Alpine repo probably doesn't have the ngx_security_headers module but, the mentioned tutorial also provides an option of using Headers More module. You should be able to install this module in your alpine distro using the command:

apk add nginx-mod-http-headers-more

Hope it helps.

Source


I found the alternate solution. The reason that it shows binary not compatible is because I have one nginx pre-installed under the target route, and it is not compatible with the header-more module I am using. That means I cannot simply install the third party library from Alpine package.

So I prepare a clean Alpine OS, and follow the GitHub repository to build Nginx from the source with additional feature. The path of build result is the prefix path you specified.