API7 Docs
General

Error Page

The error-page plugin customizes gateway-generated 404, 500, 502, and 503 responses without modifying responses returned by upstream services.

The error-page plugin customizes gateway-generated 404, 500, 502, and 503 responses. It does not modify responses returned by upstream services.

Enable the Plugin

API7 Gateway loads error-page by default. In APISIX, load the plugin in the gateway static configuration before configuring its metadata.

Keep the existing plugin list in config.yaml and add error-page:

config.yaml
plugins:
  # Keep the existing plugin list.
  - error-page

Reload APISIX for the change to take effect.

Example

Customize Error Page

The example demonstrates how to customize a gateway-generated 404 response with plugin metadata.

Configure the plugin metadata for customized error page:

curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/error-page" -X PUT \
  -H "X-API-KEY: ${ADMIN_API_KEY}" \
  -d '{
    "enable": true,
    "error_404": {
      "body": "<html>\n  <head>\n    <title>404</title>\n  </head>\n  <body>\n    <center>\n      <h1>404 not found</h1>\n    </center>\n    <hr>\n    <center>Gateway</center>\n  </body>\n</html>",
      "content_type": "text/html"
    }
  }'

To demonstrate the function of the plugin, create a route with the serverless-post-function plugin, which returns a 404 error code from gateways for all requests to the route:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
  -H "X-API-KEY: ${ADMIN_API_KEY}" \
  -d '{
    "uri": "/*",
    "id": "error-page-route",
    "plugins": {
      "serverless-post-function": {
        "functions": [
          "return function (conf, ctx)
              local core = require(\"apisix.core\")
              core.response.exit(404)
          end"
        ]
      },
      "error-page": {}
    },
    "upstream": {
      "nodes": {
        "httpbin.org:80": 1
      },
      "type": "roundrobin"
    }
  }'

❶ Return a 404 status code for all requests to the route.

❷ Enable error-page to return the customized error page.

Send a request to the route:

curl -i "http://127.0.0.1:9080/get"

You should see an HTTP/1.1 404 Not Found response with the following response body:

<html>
  <head>
    <title>404</title>
  </head>
  <body>
    <center>
      <h1>404 not found</h1>
    </center>
    <hr>
    <center>Gateway</center>
  </body>
</html>