Redirect a client ΒΆ
The Nais platform support two ways to exposing your application, either via an Ingress resource, or through service discovery. This means that we also support two ways to redirect your application.
Ingress ΒΆ
To redirect traffic from one domain to another, you need to define an ingress from the old domain that redirects to the
new domain, with .spec.redirects[].
The redirects field specifies URL redirects. It is structured as a mapping from a source URL to a target URL.
A successful redirect will return a 302 Moved Temporarily HTTP status code with location header set to the target URL.
- The status code can not be overridden.
- The
fromandtoURLs must be valid URLs. - The
toURL must be a valid URL that is exposed by the application.
Explicit redirect permission
If the redirection is between two teams/namespaces, the following annotation needs to be set on the old Nais application
metadata:
annotations:
nais.io/allow-redirect: "true"Structure ΒΆ
apiVersion: nais.io/v1alpha1
kind: Application
spec:
ingress:
- https://<MY-SUBDOMAIN-NEW>.<ENVIRONMENT-DOMAIN>
redirects:
- from: https://<MY-SUBDOMAIN-OLD>.<ENVIRONMENT-DOMAIN>
to: https://<MY-SUBDOMAIN-NEW>.<ENVIRONMENT-DOMAIN>Parameters ΒΆ
- from: (string) The source URL for the redirection. This is the URL that will be redirected.
- to: (string) The target URL for the redirection. This is the URL that the client will be redirected to.
Usage Example ΒΆ
apiVersion: nais.io/v1alpha1
kind: Application
spec:
ingress:
- http://example-new.nais.io
redirects:
- from: "http://example-old.nais.io"
to: "http://example-new.nais.io"In this example:
- Requests to
http://example-old.nais.ioare redirected tohttp://example-new.nais.io.
Service discovery ΒΆ
To redirect traffic coming through service discovery you need to create a service in the old namespace pointing to the new app/service in the new namespace. This is not a part of the Nais application specification, so you need to create and deploy a separate file for this.
Access policy
Remember that access policy is traffic between pods, so if you redirect the traffic to a different namespace, the client needs to update their access policy to use the new namespace.
Structure ΒΆ
apiVersion: v1
kind: Service
metadata:
name: old-appname-redirect # Replace
namespace: old-namespace # Replace
spec:
type: ExternalName
ports:
- port: 443
externalName: old-appname.old-namespace.svc.cluster.local # ReplaceDeploy either through Github Actions, or using kubectl apply -f .nais/service.yaml.