From 1e51e7151d67614bb90e253c1d6ce26bd64365ea Mon Sep 17 00:00:00 2001 From: YuryHrytsuk Date: Tue, 7 Jan 2025 16:02:45 +0100 Subject: [PATCH] Updates --- services/traefik/docker-compose.yml.j2 | 11 +++++++++-- services/traefik/j2cli_customization.py | 3 +-- services/traefik/template.env | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/services/traefik/docker-compose.yml.j2 b/services/traefik/docker-compose.yml.j2 index f67a4bf0..a19a23a3 100644 --- a/services/traefik/docker-compose.yml.j2 +++ b/services/traefik/docker-compose.yml.j2 @@ -152,20 +152,27 @@ services: ### {% set redirect_from_domains_list = TRAEFIK_DOMAINS_REDIRECT_FROM.split(',') %} {% set redirect_to_domains_list = TRAEFIK_DOMAINS_REDIRECT_TO.split(',') %} +{% set redirect_is_permanent_list = TRAEFIK_DOMAINS_REDIRECT_IS_PERMANENT.split(',') %} -{% for from_domain, to_domain in zip(redirect_from_domains_list, redirect_to_domains_list) %} +{% for ix in range(redirect_from_domains_list | length) %} + +{% set from_domain = redirect_from_domains_list[ix] %} {% set from_domain_no_dots = from_domain.replace(".", "-") %} +{% set to_domain = redirect_to_domains_list[ix] %} +{% set redirect_is_permanent = redirect_is_permanent_list[ix] %} + # Regex below is redirecting any subdomains and path to new domain. # Use https://regex101.com/r/58sIgx/2 for regex explanation and experimentation. # Below we include dollar escaping and j2 expressions. It is not clean / pure regex # You can fetch baked and clean regex from traefik dashboards. - traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.regex=^https?://((?:[a-zA-Z0-9-]+\.)*)*{{ from_domain }}(.*)$$ - traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.replacement=https://$${1}{{ to_domain }}$${2} - - traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.permanent=false + - traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.permanent={{ redirect_is_permanent }} - traefik.http.routers.{{ from_domain_no_dots }}.rule={{ generate_domain_capture_all_rule(from_domain) }} - traefik.http.routers.{{ from_domain_no_dots }}.middlewares=redirect-{{ from_domain_no_dots }} - traefik.http.routers.{{ from_domain_no_dots }}.entrypoints=https - traefik.http.routers.{{ from_domain_no_dots }}.tls=true + {% endfor %} networks: diff --git a/services/traefik/j2cli_customization.py b/services/traefik/j2cli_customization.py index ae761418..aafdad4c 100644 --- a/services/traefik/j2cli_customization.py +++ b/services/traefik/j2cli_customization.py @@ -17,7 +17,6 @@ def _generate_domain_capture_all_rule(domain: str) -> str: def j2_environment(env): env.globals.update( - generate_domain_capture_all_rule=_generate_domain_capture_all_rule, - zip=zip, + generate_domain_capture_all_rule=_generate_domain_capture_all_rule ) return env diff --git a/services/traefik/template.env b/services/traefik/template.env index 22ab9658..c707b1b7 100644 --- a/services/traefik/template.env +++ b/services/traefik/template.env @@ -40,3 +40,4 @@ WEBSERVER_PORT=${WEBSERVER_PORT} TRAEFIK_DOMAINS_REDIRECT_FROM=${TRAEFIK_DOMAINS_REDIRECT_FROM} TRAEFIK_DOMAINS_REDIRECT_TO=${TRAEFIK_DOMAINS_REDIRECT_TO} +TRAEFIK_DOMAINS_REDIRECT_IS_PERMANENT=${TRAEFIK_DOMAINS_REDIRECT_IS_PERMANENT}