Support rewriting status codes in error page middleware

This commit is contained in:
Daniel Peinhopf 2025-03-03 11:54:04 +01:00 committed by GitHub
parent f0849e8ee6
commit fa76ed57d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 195 additions and 8 deletions

View file

@ -69,6 +69,30 @@ func (s *ErrorPagesSuite) TestErrorPage() {
require.NoError(s.T(), err)
}
func (s *ErrorPagesSuite) TestStatusRewrites() {
// The `statusRewrites.toml` file contains a misconfigured backend host and some status code rewrites.
file := s.adaptFile("fixtures/error_pages/statusRewrites.toml", struct {
Server1 string
Server2 string
}{s.BackendIP, s.ErrorPageIP})
s.traefikCmd(withConfigFile(file))
frontendReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8080", nil)
require.NoError(s.T(), err)
frontendReq.Host = "test502.local"
err = try.Request(frontendReq, 2*time.Second, try.BodyContains("An error occurred."), try.StatusCodeIs(404))
require.NoError(s.T(), err)
frontendReq, err = http.NewRequest(http.MethodGet, "http://127.0.0.1:8080", nil)
require.NoError(s.T(), err)
frontendReq.Host = "test418.local"
err = try.Request(frontendReq, 2*time.Second, try.BodyContains("An error occurred."), try.StatusCodeIs(400))
require.NoError(s.T(), err)
}
func (s *ErrorPagesSuite) TestErrorPageFlush() {
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("Transfer-Encoding", "chunked")

View file

@ -0,0 +1,45 @@
[global]
checkNewVersion = false
sendAnonymousUsage = false
[log]
level = "DEBUG"
noColor = true
[entryPoints]
[entryPoints.web]
address = ":8080"
[providers.file]
filename = "{{ .SelfFilename }}"
## dynamic configuration ##
[http.routers]
[http.routers.router1]
rule = "Host(`test502.local`)"
service = "service1"
middlewares = ["error"]
[http.routers.router2]
rule = "Host(`test418.local`)"
service = "noop@internal"
middlewares = ["error"]
[http.middlewares]
[http.middlewares.error.errors]
status = ["500-502", "503-599", "418"]
service = "error"
query = "/50x.html"
[http.middlewares.error.errors.statusRewrites]
"418" = 400
"500-502" = 404
[http.services]
[http.services.service1.loadBalancer]
passHostHeader = true
[[http.services.service1.loadBalancer.servers]]
url = "http://{{.Server1}}:8989474"
[http.services.error.loadBalancer]
[[http.services.error.loadBalancer.servers]]
url = "http://{{.Server2}}:80"

View file

@ -1003,6 +1003,8 @@ spec:
description: |-
Query defines the URL for the error page (hosted by service).
The {status} variable can be used in order to insert the status code in the URL.
The {originalStatus} variable can be used in order to insert the upstream status code in the URL.
The {url} variable can be used in order to insert the escaped request URL.
type: string
service:
description: |-
@ -1199,6 +1201,13 @@ spec:
items:
type: string
type: array
statusRewrites:
additionalProperties:
type: integer
description: |-
StatusRewrites defines a mapping of status codes that should be returned instead of the original error status codes.
For example: "418": 404 or "410-418": 404
type: object
type: object
forwardAuth:
description: |-