6

Traefik ingress controller has been supporting traefik.frontend.rule.type: PathPrefixStrip for quite some time, which is useful when a root path of a microservice needs to be available at example.com/path/.

Here is how an example yaml with a manifest looks like:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example
  annotations:
    traefik.frontend.rule.type: PathPrefixStrip
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /path/
        backend:
          serviceName: example
          servicePort: http

The problem with this approach is that it does not add a trailing slash when a client goes to example.com/path – if I understand correctly, this can only be achieved with an extra ingress rule.

More recent versions of traefik support a wider set of annotations, which suggests that the addition of a trailing slash may be declared inside just one rule.

Here is my attempt to solve this in traefik 1.7:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example
  annotations:
    traefik.ingress.kubernetes.io/redirect-permanent: "true"
    traefik.ingress.kubernetes.io/redirect-regex: https?://example.com/path$
    traefik.ingress.kubernetes.io/redirect-replacement: https://example.com/path/
    traefik.ingress.kubernetes.io/request-modifier: "ReplacePathRegex: ^/path/(.*) /$1"
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /path
        backend:
          serviceName: example
          servicePort: http

This works, but I'm not sure if the solution is the most elegant and performant. What could be simplified or improved? Is it possible to generalize the regexps to make copy-pasting easier?

Here's the goal, just to recap:

http://example.com/path → 301 to http://example.com/path/
http://example.com/path/ → example microservice, path /
http://example.com/path/abcde → example microservice, path /abcde

4

0 回答 0