Skip to content

Add way to serve custom error pages via traefik middleware #305

Open
@niklasgrewe

Description

@niklasgrewe

On my debian server i am using exoframe and traefik. I have services that i deployed with exoframe and there are some apps like directus or gitlab that i added manually. So far so good. Everything works fine. But if a service is not available, only the following text is displayed in the browser:

404 page not found

of course it doesn't look that professional... so I would like to display my own error pages, if

  1. the service is not available or
  2. when the server is unavailable for e.g. maintaince to install updates which requires reboot etc.

I read in the traefik documentation that you need an own service for the error pages. So I found this example:

version: '3'

networks:
  proxy:
    external: true

services:
  reverse-proxy:
    # The official v2.0 Traefik docker image
    image: traefik:v2.0
    # Enables the web UI and tells Traefik to listen to docker
    networks:
      - proxy
    ports:
      # The HTTP port
      - 80:80
      - 443:433
      # The Web UI localhost:8080 (enabled by --api.insecure=true)
      - 8080:8080
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
    command:
      - --log.level=DEBUG
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
    labels:
      - "traefik.enable=true"
      - "traefik.http.middlewares.secured.chain.middlewares=app-basic-auth,error-page"
      - "traefik.http.middlewares.app-basic-auth.basicauth.users=test:$$apr1$$8nyeo2br$$eaGzBj0gKvjNiu7Ch7vl3." # test : test (use escape for generate 401 hope custome page)
      - "traefik.http.middlewares.error-page.errors.status=400-599"
      - "traefik.http.middlewares.error-page.errors.service=error-page@docker"
      - "traefik.http.middlewares.error-page.errors.query=/{status}.html"

  # Container used to serve custom error page to replace 502 Bad gateway error with a awesome page.
  # If you try localhost or unknown url, this container serve content in /htdocs but don't serve 404.html or others curtom page. I think have missed something.
  apache-error-page:
    image: httpd:alpine
    container_name: apache-error-page
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    volumes:
      # [!] create this folder with some html page for testing
      - ./errors:/usr/local/apache2/htdocs/ #contain my custom errors pages 401.html 404.html 502.html ...
    labels:
      - "traefik.enable=true"
      - "traefik.http.services.error-page.loadbalancer.server.port=80"

How can I apply the example shown above to my exoframe environment? Do i have to specify a separate service in each docker-compose file that displays the error pages, or can i define one globally? if so, how? Where do I have to specify which labels so that Traefik displays the error pages even if the desired service is not available?

What if the whole server is not available. Can I still display my own error pages even in this case? Can Traefik then address a backend server, which then displays the error pages? What do I have to configure here to make this work?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions