Skip to content

Commit

Permalink
Traefik: introduce domain redirect (1st version) (#925)
Browse files Browse the repository at this point in the history
* Traefik: introduce domain redirect

* Fix env vars

* Update CI deps

* Updates

* Revert CI changes
  • Loading branch information
YuryHrytsuk authored Jan 8, 2025
1 parent 6f90f13 commit f9077d3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
29 changes: 29 additions & 0 deletions services/traefik/docker-compose.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ services:
- traefik.http.middlewares.strip-www.redirectregex.replacement=$${1}://$${2}
- traefik.http.middlewares.strip-www.redirectregex.permanent=true
###
# Domain redirects
###
{% 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 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={{ 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:
public: null
monitored: null
Expand Down
22 changes: 22 additions & 0 deletions services/traefik/j2cli_customization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def _generate_domain_capture_all_rule(domain: str) -> str:
rules = [
f"Host(`{domain}`)",
f"Host(`www.{domain}`)",
f"Host(`invitations.{domain}`)",
f"Host(`services.{domain}`)",
f"HostRegexp(`{{subhost:[a-zA-Z0-9-]+}}.services.{domain}`)",
f"Host(`services.testing.{domain}`)",
f"HostRegexp(`{{subhost:[a-zA-Z0-9-]+}}.services.testing.{domain}`)",
f"Host(`pay.{domain}`)",
f"Host(`api.{domain}`)",
f"Host(`api.testing.{domain}`)",
f"Host(`testing.{domain}`)",
]
return " || ".join(rules)


def j2_environment(env):
env.globals.update(
generate_domain_capture_all_rule=_generate_domain_capture_all_rule
)
return env
4 changes: 4 additions & 0 deletions services/traefik/template.env
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ MONITORED_NETWORK=${MONITORED_NETWORK}

WEBSERVER_HOST=${WEBSERVER_HOST}
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}

0 comments on commit f9077d3

Please sign in to comment.