Custom middlewares

Traefik middleware changes a request before it reaches a resource. Common uses include rate limits, response headers, IP allow lists, redirects, and external authentication.

How you attach middleware depends on the resource type. Standard applications can use generated labels or manually edited labels. Docker Compose applications and services can use Coolify's coolify.traefik.middlewares shorthand.

Keep labels managed by Coolify when possible

Choosing Managed manually (edit labels yourself) in Label management stops Coolify from regenerating application labels. You become responsible for every router, middleware, service, and certificate label for the application. Use dynamic configuration with a small reference label when it can meet the requirement.

Define reusable middleware

Open Servers > your server > Proxy > Dynamic Configurations and create a file such as shared-middlewares.yaml. Start with the example that matches the request change you need. You can combine multiple middleware definitions in the same file.

http:
  middlewares:
    app-rate-limit:
      rateLimit:
        average: 100
        period: 1m
        burst: 50

The @file suffix identifies middleware loaded from Traefik's file provider.

Attach middleware to a resource

Open the application General page and find Container labels. Set Label management to Managed manually (edit labels yourself), then append the middleware names to the existing HTTPS router label:

traefik.http.routers.https-0-<resource-id>.middlewares=gzip 
traefik.http.routers.https-0-<resource-id>.middlewares=gzip,app-rate-limit@file,security-headers@file 

Keep every existing middleware name. Save and redeploy the application.

If the route breaks, select Reset to defaults, set Label management back to Managed by Coolify (auto-generated), and redeploy before trying again.

You can also define middleware with Docker labels. For example:

labels: 
  - 'traefik.http.middlewares.app-rate-limit.ratelimit.average=100'
  - 'traefik.http.middlewares.app-rate-limit.ratelimit.period=1m'
  - 'traefik.http.middlewares.app-rate-limit.ratelimit.burst=50'
  - 'coolify.traefik.middlewares=app-rate-limit'

Use file-based configuration when several resources share the same middleware. Use labels when the middleware belongs to one Compose service.


Redirects

Choose the redirect that matches the result you need.

Standard applications have a built-in www redirect setting on Domains. Add both hostnames, keep labels managed by Coolify, then choose one of:

www redirectResult
No redirectBoth configured hostnames remain available.
Redirect to wwwThe non-www hostname redirects to the configured www hostname.
Redirect to non-wwwThe www hostname redirects to the configured non-www hostname.

Select Save changes and redeploy if the running container does not yet have the regenerated labels. Both hostnames also need DNS records that point to the server.

Docker Compose applications and services

Add both domains to the resource, then add the redirect middleware labels to the service that receives them.

labels: 
  - 'traefik.http.middlewares.example-redirect.redirectregex.regex=^(http|https)://(?:www\.)?(.+)'
  - 'traefik.http.middlewares.example-redirect.redirectregex.replacement=${1}://www.${2}'
  - 'traefik.http.middlewares.example-redirect.redirectregex.permanent=true'
  - 'coolify.traefik.middlewares=example-redirect'

These examples assume Escape special characters in labels? remains enabled. If you disable it, write each $ as $$ in the source Compose file.

Troubleshooting

On this page