호스트 재작성
호스트 재작성 (Rewriting hosts)
Kong Ingress Controller에서 제공하는 어노테이션으로 요청의 Host 헤더를 조작하는 방법을 소개해요. 사용자가 보낸 Host를 그대로 유지하거나, 업스트림 서비스의 Host로 바꾸거나, 원하는 값으로 직접 설정하는 세 가지 동작을 배울 수 있어요. Gateway API와 Ingress 각각에 맞는 설정 예시도 함께 담겨 있어요
출처: 문서
본문
- 사용자가 제공한
Host헤더를 유지해요 - 업스트림 서비스의
Host를 기본값으로 사용해요 Host헤더를 알려진 값으로 명시적으로 설정해요
Host 헤더 유지하기
Kong Ingress Controller는 기본적으로 요청에 담긴 호스트명을 그대로 유지해요.
curl -H 'Host:kong.example' "$PROXY_IP/echo?details=true"
HTTP request details
---------------------
Protocol: HTTP/1.1
Host: kong.example
Method: GET
URL: /?details=true
업스트림으로 전달되는 요청의 Host 헤더는 Kong Gateway로 보낸 요청의 Host 헤더와 일치해요.
업스트림 Host 이름 사용하기
Host 헤더가 서비스의 업스트림 호스트명을 담도록 하려면 preserve-host를 비활성화하면 돼요.
Route에 konghq.com/preserve-host 어노테이션을 추가해요:
Gateway API
kubectl patch httproute NAME --type merge -p '{"metadata":{"annotations":{"konghq.com/preserve-host":"false"}}}'
Ingress
kubectl patch ingress NAME -p '{"metadata":{"annotations":{"konghq.com/preserve-host":"false"}}}'
이제 응답의 Host 헤더에는 업스트림 호스트와 포트가 담겨요.
HTTP request details
---------------------
Protocol: HTTP/1.1
Host: 192.168.194.11:1027
Method: GET
URL: /?details=true
Host 헤더 명시적으로 설정하기
Gateway API v3.2+ 사용하기
Gateway API의 HTTPRoute를 사용할 때는 URLRewrite
필터의 hostname 필드로 Host 헤더를 명시적으로 설정할 수 있어요. HTTPRoute 규칙에 URLRewrite 필터만 추가하면 돼요.
...
filters:
- type: URLRewrite
urlRewrite:
hostname: internal.myapp.example.com
konghq.com/host-header 어노테이션 사용하기
필요하다면 konghq.com/preserve-host를 비활성화하고 konghq.com/host-header 어노테이션을 설정해서 Host 헤더를 명시적으로 지정할 수 있어요.
-
Ingress에
konghq.com/preserve-host어노테이션을 추가해서preserve-host를 비활성화하고,host-header어노테이션에 지정한 호스트명을 보내도록 해요:Gateway API
kubectl patch httproute NAME --type merge -p '{"metadata":{"annotations":{"konghq.com/preserve-host":"false"}}}'Ingress
kubectl patch ingress NAME -p '{"metadata":{"annotations":{"konghq.com/preserve-host":"false"}}}' -
Service에
konghq.com/host-header어노테이션을 추가하면Host헤더를 직접 설정해요:kubectl patch service NAME -p '{"metadata":{"annotations":{"konghq.com/host-header":"internal.myapp.example.com"}}}' -
Host헤더를 넣어서curl요청을 보내요:curl -H 'Host:kong.example' "$PROXY_IP/echo?details=true"이제 업스트림으로 가는 요청은
host-header어노테이션의 헤더를 사용해요:HTTP request details --------------------- Protocol: HTTP/1.1 Host: internal.myapp.example.com:1027 Method: GET URL: /?details=true
관련 리소스
더 알아보기 (Learn more)
- Host 헤더 유지 여부를 제어하는 어노테이션의 상세 레퍼런스를 확인할 수 있어요 —
konghq.com/preserve-hostannotation - konghq.com/rewrite 어노테이션으로 경로를 재작성하는 방법을 안내해요 — Using the konghq.com/rewrite annotation
- 경로 조작의 전반적인 개념과 활용 방법을 정리한 문서예요 — Rewriting paths