경로 재작성
경로 재작성 (Rewriting paths)
Kong Ingress Controller의 기본 경로 처리 동작을 바꾸고 싶을 때 활용할 수 있는 옵션들을 소개해 드려요. strip-path 어노테이션으로 경로 접두사를 제거하거나, Gateway API의 URLRewrite 필터와 konghq.com/rewrite 어노테이션, 정규식으로 경로를 재작성하는 방법을 다뤄요. 각 방식별 설정 예시와 함께 요청 경로가 업스트림에서 어떻게 변하는지 예제로 확인할 수 있어요
출처: 문서
본문
strip-path를 사용해서 경로 접두사를 제거해요- v3.2+부터는 Gateway API의
URLRewrite필터를 사용해서 재작성해요 - 정규 표현식을 사용해서 재작성해요
path어노테이션을 사용해서 경로 접두사를 추가해요
경로 제거하기
이건 Kong Ingress Controller의 기본 동작이에요. 이 동작을 비활성화하려면
konghq.com/strip-path="false"로 설정하세요
Ingress에 konghq.com/strip-path 어노테이션을 추가하면 Route/Ingress의 경로 컴포넌트가 제거되고, 나머지 경로는 루트에 남게 돼요:
Gateway API
kubectl patch httproute NAME --type merge \
-p '{"metadata":{"annotations":{"konghq.com/strip-path":"true"}}}'
Ingress
kubectl patch ingress NAME --type merge \
-p '{"metadata":{"annotations":{"konghq.com/strip-path":"true"}}}'
라우팅 규칙에 path: /user가 포함되어 있다면, GET /user/details 요청은 라우팅 규칙의 경로가 제거된 채 업스트림에는 GET /details 요청으로 전달돼요.
/
원본 요청: /example/here 업스트림 요청: /example/here
/example
원본 요청: /example/here 업스트림 요청: /here
/example/here
원본 요청: /example/here 업스트림 요청: /
경로 재작성하기
많은 경우 경로 접두사를 제거하는 것만으로는 부족해요. 내부 시스템에는 외부에 공개하기 적합하지 않은 URL이 들어 있거든요. Kong Ingress Controller는 외부 소비자에게 사용자 친화적인 인터페이스를 제공할 수 있도록 URL을 투명하게 재작성할 수 있어요.
konghq.com/rewrite 어노테이션 사용하기
이 기능을 사용하려면
RewriteURIs기능 게이트가 활성화되어 있어야 하고,Ingress리소스에서만 동작해요
Ingress에 konghq.com/rewrite 어노테이션을 추가하면 업스트림 요청에 사용할 특정 경로를 설정할 수 있어요. Route 정의에 정의된 정규식 매치도 모두 사용할 수 있어요 (자세한 내용은 어노테이션 문서를 참고하세요):
kubectl patch ingress NAME --type merge \
-p '{"metadata":{"annotations":{"konghq.com/rewrite":"/hello/world"}}}'
들어오는 요청의 쿼리 문자열 파라미터는 그대로 유지돼요.
/
업스트림 요청: /hello/world
/example
업스트림 요청: /hello/world
/example?demo=true
업스트림 요청: /hello/world?demo=true
konghq.com/rewrite 어노테이션을 정규 표현식과 함께 사용하면 입력 파라미터를 캡처해서 업스트림으로 전달할 수 있어요. 자세한 내용은 konghq.com/rewrite 어노테이션으로 경로 재작성하기 how-to 문서를 참고하세요.
Gateway API 필터 사용하기
HTTPRoute에 path.replaceFullPath를 사용하는 URLRewrite 필터를 추가하면 요청의 전체 경로를 교체할 수 있어요.
...
filters:
- type: URLRewrite
urlRewrite:
path:
type: ReplaceFullPath
replaceFullPath: /rewritten-path
또는 HTTPRoute 규칙에 path.replacePrefixMatch를 사용하는 URLRewrite 필터를 추가해서 경로 접두사를 재작성할 수도 있어요.
자세한 내용은 URLRewrite 필터 문서를 참고하세요.
...
rules:
- matches:
- path:
type: PathPrefix # Only PathPrefix path type is supported with URLRewrite filter using path.type == ReplacePrefixMatch.
value: /old-prefix
filters:
- type: URLRewrite
urlRewrite:
path:
type: ReplacePrefixMatch
replacePrefixMatch: /new-prefix
경로 앞에 추가하기
Service에 konghq.com/path 어노테이션을 추가하면 해당 값이 업스트림 경로 앞에 붙어요:
kubectl patch service NAME -p '{"metadata":{"annotations":{"konghq.com/path":"/api"}}}'
/
업스트림 요청: /api
/example
업스트림 요청: /api/example
/example?demo=true
업스트림 요청: /api/example?demo=true
관련 리소스
더 알아보기 (Learn more)
- 경로 접두사 제거 동작을 제어하는 어노테이션의 상세 레퍼런스예요 —
konghq.com/strip-pathannotation - konghq.com/rewrite 어노테이션 사용을 위해 활성화해야 하는 기능 게이트를 설명해요 —
RewriteURIsfeature gate - konghq.com/rewrite 어노테이션과 정규식으로 경로를 재작성하는 실습 가이드예요 — rewriting paths with the konghq.com/rewrite annotation
- 경로가 아닌 호스트를 재작성하는 방법을 다루는 문서예요 — Rewriting hosts