본문 바로가기
WIKI 기술 지식 베이스

RedirectRegex

원문 보기 위키 갱신

출처: Traefik RedirectRegex Documentation

본문

RedirectRegex

RedirectRegex는 정규식 매칭과 치환을 사용해 요청을 리다이렉트합니다.

구성 예시

구조화된 (YAML)

# 도메인 치환으로 리다이렉트
http:
  middlewares:
    test-redirectregex:
      redirectRegex:
        regex: "^http://localhost/(.*)"
        replacement: "http://mydomain/${1}"

구조화된 (TOML)

# 도메인 치환으로 리다이렉트
[http.middlewares]
  [http.middlewares.test-redirectregex.redirectRegex]
    regex = "^http://localhost/(.*)"
    replacement = "http://mydomain/${1}"

Labels

# 도메인 치환으로 리다이렉트
# 참고: 모든 달러 기호는 이스케이프를 위해 두 배로 적어야 합니다.
labels:
  - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
  - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"

Tags

// 도메인 치환으로 리다이렉트
// 참고: 모든 달러 기호는 이스케이프를 위해 두 배로 적어야 합니다.
{
  // ...
  "Tags" : [
    "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
    "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"
  ]
}

Kubernetes

# 도메인 치환으로 리다이렉트
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: test-redirectregex
spec:
  redirectRegex:
    regex: ^http://localhost/(.*)
    replacement: http://mydomain/${1}

구성 옵션

| Field | Description | Default | Required | | regex | regex 옵션은 요청 URL에서 요소를 매칭하고 캡처하는 정규식입니다. | "" | Yes | | permanent | 영구 리다이렉션 활성화. | false | No | | replacement | replacement 옵션은 새 대상 URL을 갖도록 URL을 어떻게 수정할지 정의해요. $1x는 ${1x}와 동등하며 ${1}x가 아닙니다(Regexp.Expand 참고). 따라서 ${1} 문법을 사용하세요. | "" | No |

regex

regex 옵션은 요청 URL에서 요소를 매칭하고 캡처하는 정규식입니다.

Tip

정규식과 치환은 Go Playground나 Regex101 같은 온라인 도구로 테스트할 수 있어요.

YAML 안에서 정규식을 정의할 때는 이스케이프된 문자를 두 번 이스케이프해야 합니다: example\.com은 example\\\\.com으로 적어야 해요.

replacement

replacement 옵션은 새 대상 URL을 갖도록 URL을 어떻게 수정할지 정의합니다.

Warning

치환 확장 변수를 정의할 때 주의하세요: $1x는 ${1x}와 동등하며 ${1}x가 아닙니다(Regexp.Expand 참고). 따라서 ${1} 문법을 사용하세요.

Traefik OSS를 프로덕션에서 사용하시나요?

직장에서 Traefik을 사용한다면, 엔터프라이즈급 API 게이트웨이 기능이나 Traefik OSS 상용 지원을 추가하는 것을 고려해 보세요.

  • API Gateway 데모 영상 보기

  • 24/7/365 OSS 지원 요청

Traefik OSS에 API 게이트웨이 기능을 추가하는 것은 빠르고 매끄러워요. 기존 구성을 교체하거나(rip and replace) 버릴 필요 없이 그대로 유지됩니다. 짧은 영상으로 직접 확인해 보세요.

더 알아보기 (Learn more)