Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect in Traefik from one domain to another

According to the Traefik 1.7 documentation you should be able to have Traefik perform a 302 redirect using:

  • traefik.ingress.kubernetes.io/redirect-regex
  • traefik.ingress.kubernetes.io/redirect-replacement

My goal is to simply remove the www. from the address.

This is what I've tried, but I get a 404 service not found.

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: www-redirect
  namespace: public
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/redirect-regex: ^https?://www.example.com/(.*)
    traefik.ingress.kubernetes.io/redirect-replacement: https://example.com/$1
spec:
  rules:
  - host: www.example.com

Unfortunately the documentation isn't explicit on how to use them. At the time of writing the only google hit on this is the documentation (above).

My current work around (assuming it'll help explain the question) is to route www. traffic to nginx which returns a 302.

server {
    listen       80;
    server_name  www.example.com;
    return 302 https://example.com$request_uri;
}

This seems like overkill.

like image 518
DanielM Avatar asked Feb 05 '18 17:02

DanielM


1 Answers

I was having the same issue and ended up making it work with:

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: www-redirect
  namespace: public
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/preserve-host: "true"
    traefik.ingress.kubernetes.io/redirect-permanent: "true"
    traefik.ingress.kubernetes.io/redirect-regex: "^https://www.(.*)"
    traefik.ingress.kubernetes.io/redirect-replacement: "https://$1"
spec:
  tls:
    - hosts:
        - "example.com"
        - "www.example.com"
      secretName: example-tls
  rules:
  - host: example.com
  - host: www.example.com

Basically I needed both rules.

As a side note, I also start the trafik pod with the following flags:

args:
   - --api
   - --kubernetes
   - --logLevel=INFO
   - --entryPoints=Name:https Address::443 TLS
   - --entrypoints=Name:http Address::80 Redirect.EntryPoint:https
   - --defaultentrypoints=https,http
like image 195
Sylvain Hellegouarch Avatar answered Nov 08 '22 12:11

Sylvain Hellegouarch