포스트

headers-more-nginx-module docker image

headers-more-nginx-module docker image

이 글은 Tistory에서 이전된 포스팅입니다. 원본은 여기에서 확인할 수 있습니다.

headers-more-nginx-module

nginx에서 특정 헤더에 값을 수정할 수 있는 모듈이다.

nginx에 포함되지 않은 third-party 모듈로 별도 설치가 필요하다.

도커로 nginx를 사용할 때 외부 모듈을 사용하려면, 모듈이 설치된 도커 이미지를 만들어야 한다.

고맙게도 nginx 공식 repo에서 작성해 둔 Dockerfile이 있다.

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

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

(자세한 내용은 repo README 참고)

Dockerfile을 복사하거나 다운받고, pkg-oss repository에서 설치 가능한 외부 모듈을 인자로 설정하여 빌드

+) 도커 이미지를 빌드할 때, 서버 인스턴스에서 돌리면 메모리 부족으로 에러가 나올 수 있다.

리소스가 넉넉한 환경에서 빌드하고 registry에 push/pull로 사용하는 것이 좋다.

nginx 설정

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

...

http {
...
}

load_module을 잊지말고

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';
}

리버스 프록시 역할을 하는 nginx의 경우, 서버 응답의 Set-Cookie 값을 수정하는 등, 헤더 value를 수정할 수 있다.

참고

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

이 기사는 저작권자의 CC BY-NC 4.0 라이센스를 따릅니다.