Customize Traefik Rules in Dokploy
30 Oct 2025Dokploy manages Traefik for you, which is great until you need something it doesn’t expose in the UI: a redirect rule, a custom header, a rate limiter. You don’t have to fight the abstraction. Traefik reads dynamic config from files, and Dokploy will happily load whatever you drop in the right directory.
How Dokploy wires up Traefik
Dokploy runs Traefik as a Docker Swarm service and generates configuration automatically from your applications’ domain settings — that’s how a new app gets HTTPS and routing with zero manual Traefik knowledge. Under the hood, it writes dynamic configuration files to a directory on the host (typically under /etc/dokploy/traefik/dynamic) that Traefik watches for changes.
That watched directory is your extension point. Anything valid you put there gets merged into Traefik’s config alongside what Dokploy generates.
1. Find the dynamic config directory
SSH into the server running Traefik and check:
ls /etc/dokploy/traefik/dynamic/
You’ll see YAML files Dokploy created per application. Don’t edit those — Dokploy will overwrite them on the next deploy. Instead, add your own file alongside them, e.g. custom-middlewares.yml.
2. Add a middleware
Say you want to force www to redirect to the bare domain. Create the file:
# /etc/dokploy/traefik/dynamic/custom-middlewares.yml
http:
middlewares:
www-redirect:
redirectRegex:
regex: "^https://www\\.(.+)"
replacement: "https://${1}"
permanent: true
This defines the middleware but doesn’t attach it to anything yet. Traefik picks up the file automatically within seconds — no restart needed, because it’s watching the directory.
3. Attach the middleware to your app
In Dokploy’s application settings, under Advanced → Traefik Labels, you can add custom Docker labels directly. Add:
traefik.http.routers.<your-router-name>.middlewares=www-redirect@file
The @file suffix matters — it tells Traefik to look for www-redirect in the file-based dynamic config, not the Docker-label-based config it also reads. Miss that suffix and you’ll get a “middleware not found” error in the Traefik logs even though the middleware clearly exists.
Common custom rules
Security headers:
http:
middlewares:
security-headers:
headers:
stsSeconds: 31536000
frameDeny: true
contentTypeNosniff: true
IP allowlist for a staging app:
http:
middlewares:
staging-allowlist:
ipAllowList:
sourceRange:
- "203.0.113.0/24"
A rate limiter for a public API:
http:
middlewares:
api-ratelimit:
rateLimit:
average: 100
burst: 50
Each gets attached the same way — add the middleware name to the router’s middlewares label, comma-separated if you’re stacking more than one.
What breaks
Two failure modes show up constantly:
- File syntax errors kill the whole dynamic config, not just your addition. Traefik logs the parse error but Dokploy’s own generated routes can start failing too if they live in the same provider. Validate YAML before you save it — a single bad indent takes down routing for apps that have nothing to do with your change.
- Dokploy redeploys can regenerate labels and clobber manual edits made through the UI. Keep custom labels in the Advanced panel (which Dokploy persists) rather than editing the underlying compose file directly, or a redeploy will silently drop them.
The actual takeaway
Don’t reach for a hand-rolled Traefik install just because Dokploy’s UI doesn’t have a checkbox for what you need. The dynamic config directory is a real, supported escape hatch — Traefik’s file provider was built exactly for this. Keep custom files separate from Dokploy’s generated ones, use @file when referencing them from labels, and validate YAML before saving so one typo doesn’t take every app on the server offline.