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

Exit Transformer로 404 오류 메시지 커스터마이즈하기

원문 보기 위키 갱신

Exit Transformer로 404 오류 메시지 커스터마이즈하기 (Customize the 404 error message)

Exit Transformer 플러그인을 설정하면 404 응답을 변환해 커스텀 오류 메시지를 담아 돌려줄 수 있어요. 이 문서에서는 handle_unknown 파라미터를 사용해 전역 설정된 플러그인에서 404 오류가 발생했을 때 응답 본문을 바꾸는 방법을 소개해요. decK, Admin API, Konnect API 세 가지 방식으로 플러그인을 설정하는 예시를 단계별로 확인할 수 있어요. 시작하기 전에 kong.conf에서 untrusted_lua 값을 on 또는 sandbox로 설정해야 한다는 점만 기억해 주세요

출처: 문서

본문

404 오류 메시지 사용자 지정v1.3+

플러그인을 구성해서 404 응답을 변환하고 사용자 지정 오류 메시지를 추가해요.

이 예제는 handle_unknown 파라미터를 사용해요. 이 파라미터는 404 오류가 발생했을 때 전역으로 구성된 플러그인 인스턴스에만 적용돼요. 이 플러그인을 온프레미스에서 실행한다면, 기본 Workspace에서 구성해야 해요.

이 예제가 포함된 전체 방법 가이드는 404 응답 메시지 변환하기를 참고하세요.

전제 조건

  • 이 플러그인이 동작하려면 kong.conf 파일에서 untrusted_lua를 on 또는 sandbox로 설정해야 해요.

플러그인 설정하기

전역

decK

kong.yaml 구성 파일에 이 섹션을 추가하세요:

_format_version: "3.0"
plugins:
  - name: exit-transformer
    config:
      handle_unknown: true
      functions:
      - return function(status, body, headers) if status == 404 then local new_body
        = { error = true, status = status, message = "This is not the Route you are
        looking for", } return status, new_body, headers else return status, body, headers
        end end

{: data-file="kong.yaml" data-tool="deck" }

Admin API

다음 요청을 보내세요:

curl -i -X POST http://localhost:8001/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "exit-transformer",
      "config": {
        "handle_unknown": true,
        "functions": [
          "return function(status, body, headers) if status == 404 then local new_body = { error = true, status = status, message = \"This is not the Route you are looking for\", } return status, new_body, headers else return status, body, headers end end"
        ]
      }
    }
    '
Konnect API

다음 요청을 보내세요:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "exit-transformer",
      "config": {
        "handle_unknown": true,
        "functions": [
          "return function(status, body, headers) if status == 404 then local new_body = { error = true, status = status, message = \"This is not the Route you are looking for\", } return status, new_body, headers else return status, body, headers end end"
        ]
      }
    }
    '

다음 플레이스홀더는 반드시 여러분의 값으로 바꾸세요:

  • region: Kong Konnect가 호스팅되고 운영되는 지리적 지역이에요.

  • KONNECT_TOKEN: Konnect 계정과 연결된 개인 액세스 토큰(PAT)이에요.

  • controlPlaneId: 컨트롤 플레인의 id예요.

지역별 URL과 개인 액세스 토큰에 대해 알아보려면 Konnect Control Planes Config API reference를 참고하세요.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
  labels:
    global: 'true'
config:
  handle_unknown: true
  functions:
  - return function(status, body, headers) if status == 404 then local new_body =
    { error = true, status = status, message = 'This is not the Route you are looking
    for', } return status, new_body, headers else return status, body, headers end
    end
plugin: exit-transformer
" | kubectl apply -f -
Terraform
전제 조건: 개인 액세스 토큰 구성하기
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Konnect Gateway Plugin을 생성하려면 Terraform 구성에 다음 내용을 추가하세요:

resource "konnect_gateway_plugin_exit_transformer" "my_exit_transformer" {
  enabled = true

  config = {
    handle_unknown = true
    functions = ["return function(status, body, headers) if status == 404 then local new_body = { error = true, status = status, message = \"This is not the Route you are looking for\", } return status, new_body, headers else return status, body, headers end end"]
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
}

더 알아보기 (Learn more)

  • 이 예시를 전체 과정으로 따라 할 수 있는 404 응답 메시지 변환 가이드예요 — Transform a 404 response message
  • 404 응답 변환을 담당하는 handle_unknown 파라미터의 스키마 설정을 자세히 확인해 보세요 — handle_unknown
  • 플러그인에서 Lua 함수를 실행하려면 필요한 untrusted_lua 설정 방법을 설명해요 — untrusted_lua
  • decK 설정 파일 kong.yaml 작성 기본기를 먼저 익혀 보세요 — kong.yaml
  • Konnect에서 플러그인을 설정할 때 사용하는 Control Planes Config API 레퍼런스예요 — Konnect Control Planes Config API reference