Post

headers-more-nginx-module docker image

headers-more-nginx-module docker image

This post was migrated from Tistory. You can find the original here.

headers-more-nginx-module

This is a module that lets you modify the values of specific headers in nginx.

It’s a third-party module not bundled with nginx, so it needs to be installed separately.

When running nginx in Docker and you need an external module, you have to build a Docker image with that module installed.

Fortunately, the official nginx repo already provides a Dockerfile for this.

https://github.com/nginxinc/docker-nginx/tree/master/modules

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

(See the repo README for details.)

Copy or download the Dockerfile, then build it while passing in the external modules available from the pkg-oss repository as an argument.

+) When building the Docker image, running the build on a server instance can fail with an out-of-memory error.

It’s better to build it in an environment with plenty of resources and then push/pull it to/from a registry for actual use.

nginx configuration

1
2
3
4
5
6
7
8
# nginx.conf
load_module modules/ngx_http_headers_more_filter_module.so;

...

http {
...
}

Don’t forget the load_module directive.

1
2
3
4
5
6
7
8
9
10
11
12
13
location /server1 {
    include common/_cors-header.conf;
    include common/_cors-options-response.conf;
    
    proxy_pass http://server1:9999;
    proxy_set_header Host $http_host;
    proxy_set_header Origin http://$http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    more_set_headers 'Set-Cookie: $sent_http_set_cookie; Path=/; Secure; SameSite=None';
}

For an nginx instance acting as a reverse proxy, this lets you modify header values, such as rewriting the Set-Cookie value in a server’s response.

Reference

https://stackoverflow.com/questions/72806335/add-more-set-headers-to-docker-image

This post is licensed under CC BY-NC 4.0 by the author.