Traefik Routing & Middleware

Traefik is used as the edge router, providing TLS termination and global security headers for the entire environment.

🛡️ Global Middleware Patterns

1. HTTP-to-HTTPS Redirection

The system intercepts all traffic on Port 80 and issues a 301/308 Permanent Redirect to Port 443. This ensures that:

  • Browser defaults are automatically secured.
  • Backend redirects are natively upgraded to HTTPS.

2. Global CORS Configuration

A global middleware (global-cors) is attached at the websecure entrypoint. This injects Access-Control-Allow-Origin headers natively at the proxy level, so individual NestJS microservices don’t have to manage CORS logic.

🔍 Troubleshooting Routing (404 Debugging)

A critical pattern for identifying where a request fails:

Status Code FormatOriginMeaning
404 page not found (Plain Text)TraefikNo matching router. Request didn’t reach the container.
{"statusCode": 404, ...} (JSON)NestJSRequest reached the container, but no internal route exists.
{"message": "not found", ...}DatabaseRoute exists, but the specific record requested is missing.

⚙️ Example Label Configuration

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.service_name.rule=Host(`api.domain.local`) && PathPrefix(`/service-path`)"
  - "traefik.http.routers.service_name.entrypoints=websecure"
  - "traefik.http.routers.service_name.tls.certresolver=myresolver"

Source: Internal Infrastructure Manual