使用 Traefik 进行重定向
本指南介绍如何在 Coolify 中使用 Traefik 配置 URL 重定向,包括内置的重定向设置和手动中间件配置。
内置的 www / 非 www 重定向
Coolify 内置了用于 www 和非 www 重定向的 Direction 设置。当启用 Readonly labels(默认设置)时,你可以直接从应用程序设置中选择重定向行为,而无需手动编辑标签。
可用选项如下:
| Direction | 行为 |
|---|---|
| Allow both | 不重定向。www. 和 非 www. 的 URL 均可正常访问。 |
| Redirect to www | 将非 www 请求重定向到 www. 版本。 |
| Redirect to non-www | 将 www. 请求重定向到非 www 版本。 |
要使用此设置,请确保已为应用程序配置了两个 URL(例如 https://coolify.io,https://www.coolify.io),并且已启用 Readonly labels。
当禁用 Readonly labels 时,Direction 字段将变为只读状态,因为 Coolify 无法再自动生成重定向标签。在这种情况下,请按照下面的手动配置进行操作。
手动重定向配置
对于自定义重定向——或在禁用 Readonly labels 时——你需要在 Container Labels(容器标签)中使用 Traefik 的 redirectregex 中间件来配置重定向。
部署 Standard Applications 和 Docker Compose 时,配置方式略有不同。
标准应用
你需要为资源设置两个 URL(例如 https://coolify.io,https://www.coolify.io),然后添加中间件并在路由器中引用它。
www -> 非 www
# 定义重定向中间件
traefik.http.middlewares.example-redirect.redirectregex.regex=^(http|https)://www\.(.+)
traefik.http.middlewares.example-redirect.redirectregex.replacement=${1}://${2}
traefik.http.middlewares.example-redirect.redirectregex.permanent=true
# 将其添加到路由器中(追加到现有中间件)
traefik.http.routers.<unique_router_name>.middlewares=gzip,example-redirect非 www -> www
# 定义重定向中间件
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
# 将其添加到路由器中(追加到现有中间件)
traefik.http.routers.<unique_router_name>.middlewares=gzip,example-redirect<unique_router_name> 是 Coolify 为你生成的路由器名称(例如 https-0-wc04wo4ow4scokgsw8wow4s8)。你可以在现有的 Container Labels 中找到它。
域名 -> 其他域名
traefik.http.middlewares.redirect-otherdomain.redirectregex.regex=^(https?://)?source-domain\.com(.*)
traefik.http.middlewares.redirect-otherdomain.redirectregex.replacement=https://target-domain.com${2}
traefik.http.middlewares.redirect-otherdomain.redirectregex.permanent=true如果你还需要为源域名生成 SSL 证书,请为其添加一个路由器条目:
traefik.http.routers.redirect-otherdomain.middlewares=redirect-to-https,redirect-otherdomain
traefik.http.routers.redirect-otherdomain.rule=Host(`source-domain.com`) && PathPrefix(`/`)
traefik.http.routers.redirect-otherdomain.entryPoints=https
traefik.http.routers.redirect-otherdomain.tls.certresolver=letsencrypt
traefik.http.routers.redirect-otherdomain.tls=trueDocker Compose 与服务
对于 Docker Compose 部署,请在 docker-compose.yml 中定义中间件标签,并使用 coolify.traefik.middlewares 简写将其自动附加到路由器。
请确保已为资源设置了两个 URL(例如 https://coolify.io,https://www.coolify.io)。
www -> 非 www
labels:
- "traefik.http.middlewares.example-redirect.redirectregex.regex=^(http|https)://www\\.(.+)"
- "traefik.http.middlewares.example-redirect.redirectregex.replacement=$${1}://$${2}"
- "traefik.http.middlewares.example-redirect.redirectregex.permanent=true"
- "coolify.traefik.middlewares=example-redirect"非 www -> www
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"在 Docker Compose YAML 文件中,美元符号 ($) 必须转义为 $$,以防止 Docker 将其解释为环境变量引用。
调试
通过检查容器来确认 Traefik 标签是否正确应用:
# 查找你的容器 ID
docker ps
# 检查容器的标签
docker inspect <container-id>你还可以检查 Traefik 容器的日志:
docker logs -f coolify-proxy有关应用自定义中间件的更多详细信息,请参阅 自定义中间件 指南。
