Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traefik: introduce domain redirect (1st version) #925

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions services/traefik/docker-compose.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ 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(',') %}

{% for from_domain, to_domain in zip(redirect_from_domains_list, redirect_to_domains_list) %}
YuryHrytsuk marked this conversation as resolved.
Show resolved Hide resolved
{% set from_domain_no_dots = from_domain.replace(".", "-") %}
# Regex below is redirecting any subdomains and path to new domain.
# Use https://regex101.com/r/58sIgx/2 for regex explanation and experimentation.
YuryHrytsuk marked this conversation as resolved.
Show resolved Hide resolved
# 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
YuryHrytsuk marked this conversation as resolved.
Show resolved Hide resolved
- 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
23 changes: 23 additions & 0 deletions services/traefik/j2cli_customization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def _generate_domain_capture_all_rule(domain: str) -> str:
YuryHrytsuk marked this conversation as resolved.
Show resolved Hide resolved
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,
zip=zip,
)
return env
3 changes: 3 additions & 0 deletions services/traefik/template.env
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ MONITORED_NETWORK=${MONITORED_NETWORK}

WEBSERVER_HOST=${WEBSERVER_HOST}
WEBSERVER_PORT=${WEBSERVER_PORT}

TRAEFIK_DOMAINS_REDIRECT_FROM=
TRAEFIK_DOMAINS_REDIRECT_TO=
Loading