[英]Redirect unvalid subdomain to specific URL with Traefik
I would like to redirect invalid URL to a 404 error with traefik and Docker 我想使用traefik和Docker将无效的URL重定向到404错误
Example : https://sub1.domain.com is a valid doamin referenced in traefik, so nothing happen https://invalid.domain.com doesn't exist in Traefik configuration, so it will redirect to https://error.domain.com 示例: https ://sub1.domain.com是traefik中引用的有效doamin,因此Traefik配置中不存在https://invalid.domain.com ,因此它将重定向到https:// error。 domain.com
I have searched in the official documentation and the only thing I found was about redirection , but I don't know how to use them to do my purpose. 我已经搜索了官方文档,发现的唯一一件事就是关于重定向,但是我不知道如何使用它们来实现我的目的。
You should be able to do this by specifying a redirect and a replacement. 您应该能够通过指定重定向和替换来做到这一点。 You'll also need to make sure that the Host rule matches any subdomains.
您还需要确保主机规则匹配任何子域。 Here's a config for docker-compose:
这是docker-compose的配置:
services:
traefik:
# ...existing config
sub1:
# ...existing config
error:
# ...existing config
labels:
# Match all subdomains
- traefik.frontend.rule=HostRegexp:{subdomain:[a-z]+}.domain.com
- traefik.frontend.redirect.regex=^https?://(.*).domain.com
- traefik.frontend.redirect.replacement=https://error.domain.com
# Set priority to low number so matches after other rules
- traefik.frontend.priority=1
It's even easier if you don't mind about the domain changing but instead just want to show an error as you don't need the traefik.frontend.redirect
labels 如果您不介意域更改,而只是想显示一个错误,因为不需要
traefik.frontend.redirect
标签,这甚至会更加容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.