Exit Transformer: HTML로 사용자 지정 404 오류 반환하기
Exit Transformer: HTML로 사용자 지정 404 오류 반환하기 (Return custom 404 error in HTML)
이 문서는 Exit Transformer 플러그인을 사용해 404 응답을 사용자 지정 HTML과 오류 메시지로 변환하는 방법을 설명해요. handle_unknown 파라미터를 활용하는데, 이 설정은 전역으로 구성된 플러그인 인스턴스에서 404 오류가 발생했을 때만 효과가 있어요. decK, Admin API, Konnect API 세 가지 방식의 설정 예시를 제공하고, 시작 전에 kong.conf에서 untrusted_lua를 on 또는 sandbox로 설정해야 한다는 점도 짚어줘요
출처: 문서
본문
HTML로 커스텀 404 오류 반환하기v1.3+
404 응답을 커스텀 HTML로 변환하고 커스텀 오류 메시지를 추가하도록 플러그인을 설정해요.
이 예제는 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
body = [[
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>404 Not Found</title>
</head>
<body>
<h1>404</h1>
<p>Sorry, nothing to see here!</p>
</body>
</html>
]]
end
return status, body, headers
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)\n if status == 404 then\n body = [[\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>404 Not Found</title>\n</head>\n<body>\n<h1>404</h1>\n<p>Sorry, nothing to see here!</p>\n</body>\n</html>\n]]\nend\n return status, body, headers\nend"
]
}
}
'
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)\n if status == 404 then\n body = [[\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>404 Not Found</title>\n</head>\n<body>\n<h1>404</h1>\n<p>Sorry, nothing to see here!</p>\n</body>\n</html>\n]]\nend\n return status, body, headers\nend"
]
}
}
'
다음 플레이스홀더는 여러분의 값으로 바꿔야 해요:
-
region: Kong Konnect가 호스팅되고 운영되는 지리적 지역이에요. -
KONNECT_TOKEN: Konnect 계정과 연결된 개인 액세스 토큰(PAT)이에요. -
controlPlaneId: 컨트롤 플레인의id예요.
지역별 URL과 개인 액세스 토큰에 대해 알아보려면 Konnect Control Planes Config API 레퍼런스를 참고하세요.
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
body = [[
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>404 Not Found</title>
</head>
<body>
<h1>404</h1>
<p>Sorry, nothing to see here!</p>
</body>
</html>
]]
end
return status, body, headers
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 = [<<EOF
return function(status, body, headers)
if status == 404 then
body = [[
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>404 Not Found</title>
</head>
<body>
<h1>404</h1>
<p>Sorry, nothing to see here!</p>
</body>
</html>
]]
end
return status, body, headers
end
EOF]
}
control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
}
더 알아보기 (Learn more)
- 404 응답 변환 전체 과정을 단계별로 따라 할 수 있는 가이드예요 — Transform a 404 response message
handle_unknown파라미터의 스키마와 설정 옵션을 확인할 수 있어요 —handle_unknown- 플러그인이 동작하기 위한
untrusted_lua설정 방법을 안내해요 —untrusted_lua - decK용
kong.yaml구성 파일 작성 기본기를 익힐 수 있어요 —kong.yaml - Konnect API로 플러그인을 설정할 때 참고할 수 있는 API 문서예요 — Konnect Control Planes Config API reference