简体   繁体   中英

Redirect unvalid subdomain to specific URL with Traefik

I would like to redirect invalid URL to a 404 error with traefik and Docker

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM