Distinguish between HTTP error code generated by Traefik itself or by service

We are using Traefik v2.4.7 on Docker Swarm provider. We are also using custom error middleware which routes the request to nginx server (error-pages@docker service). We have the following code in traefik.yml:

http:
  middlewares:
    error-pages:
      errors:
        status:
          - "400-599"
        service: error-pages@docker
        query: "/{status}.html"

entrypoints:
  web-secure:
    address: ":443"
    http:
      middlewares:
        - error-pages@file

Our application, which is deployed behind Traefik, has some IPs whitelisted with ipwhitelist.sourcerange=... label. If it is accessed from an IP which is not allowed, error-pages middleware routes the request to /403.html in nginx service. This is expected behavior.

However, we have some cases where our application purposely returns 403 HTTP status code and displays its own "forbidden" page. We don't want Traefik middleware to route the request to nginx.

Is it possible to distinguish between HTTP error codes generated by Traefik and services themselves? If the error code is generated by Traefik, then it should go through error-pages middleware. If the error code is generated by service, the page should be returned as-is, without going through middleware. How can we achieve this?

1 Like