Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add more_set_headers to Docker image

Tags:

docker

nginx

I am trying to add a module to the base nginx container so I can set some headers. I am using the alpine image so i don't believe i can use apt-get.

This is what I have tried:

FROM nginx:stable-alpine

RUN apk update && \
apk upgrade && \
apk add nginx-mod-http-headers-more

ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf
ADD ./nginx/certs /etc/nginx/certs/self-signed

However if I add the more_set_headers key in the nginx configuration file, this nginx docker image won't start.

and in my docker-compose.yml i have the following:

version: "3.9"

services:
  nginx:
    build:
      context: .
      dockerfile: nginx.dockerfile
    depends_on:
      - php
      - mysql
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./wordpress:/var/www/html:delegated
  mysql:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: wp
      MYSQL_USER: wp
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - ./database/data:/var/lib/mysql
      - ./database/initdb.d:/docker-entrypoint-initdb.d
    ports:
      - "3306:3306"

...


like image 969
Philip Avatar asked Oct 19 '25 04:10

Philip


1 Answers

Your nginx won't start as there is likely a version mismatch between the module you're tryna load and the version of nginx running.

The module your adding at line 5 is compiled against the nginx package that ships with alpine. Not the nginx compiled from source included in the official image. The offical nginx image doesn't use the nginx pkg provided by alpine. Instead it builds from source. If you require the headers-more module. Instructions on Adding third-party modules to nginx official image can be found here: https://github.com/nginxinc/docker-nginx/tree/master/modules

If you follow the instuctions headers-more is available in pkg-oss. You can skip the compose section and just grab the Dockerfile and build with

docker build --build-arg ENABLED_MODULES="headers-more" -t my-nginx-with-headers-more .

and you should have a new image to use as such:

FROM my-nginx-with-header-more
ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf
ADD ./nginx/certs /etc/nginx/certs/self-signed

Remember to also load the module in your main nginx.conf with

load_module modules/ngx_http_headers_more_filter_module.so;
like image 110
imesias Avatar answered Oct 21 '25 22:10

imesias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!