Setup a central robots.txt service using Traefik and NGINX
mainIf you use Traefik as a reverse proxy in Docker, you can centrally serve a single /robots.txt file for all your services. This is achieved by running a lightweight service (like nginx:alpine) to host the static file and configuring a high-priority Traefik HTTP Router rule that matches the path /robots.txt regardless of the hostname.
services:
robots:
image: nginx:alpine
container_name: robots-server
volumes:
- ./static/:/usr/share/nginx/html/:ro
labels:
- "traefik.enable=true"
# Router for all /robots.txt requests
- "traefik.http.routers.robots.rule=Path(`/robots.txt`)"
- "traefik.http.routers.robots.entrypoints=web,websecure"
- "traefik.http.routers.robots.priority=3000"
- "traefik.http.routers.robots.service=robots"
- "traefik.http.routers.robots.tls.certresolver=letsencrypt"
- "traefik.http.services.robots.loadbalancer.server.port=80"
networks:
- external_network
networks:
external_network:
name: traefik_external_network
external: true