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.
Disabling Readonly labels 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: 50The @file suffix identifies middleware loaded from Traefik's file provider.
Attach middleware to a resource
Open the application General page and find Container Labels. Disable Readonly labels, 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 Labels to Defaults, re-enable Readonly labels, 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 Direction setting. Add both domains to Domains, keep Readonly labels enabled, then choose one of:
| Direction | Result |
|---|---|
| Allow www & non-www. | Both configured hostnames remain available. |
| Redirect to www. | The non-www hostname redirects to the configured www hostname. |
| Redirect to non-www. | The www hostname redirects to the configured non-www hostname. |
Select Set Direction, confirm the change, 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.
