Configuring Redirection Rules for an Nginx Ingress
Configuring a Permanent Redirection Rule
To permanently redirect an access request to a target website (status code 301), use the nginx.ingress.kubernetes.io/permanent-redirect annotation. For example, to permanently redirect all access requests to www.example.com, run the following command:
nginx.ingress.kubernetes.io/permanent-redirect: https://www.example.com
The configuration in an Nginx Ingress is as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-test
namespace: default
annotations:
nginx.ingress.kubernetes.io/permanent-redirect: https://www.example.com
spec:
rules:
- host: ''
http:
paths:
- path: /
backend:
service:
name: <your_service_name> # Replace it with the name of your target Service.
port:
number: <your_service_port> # Replace it with the port number of your target Service.
property:
ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
pathType: ImplementationSpecific
ingressClassName: nginx
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-test
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/permanent-redirect: https://www.example.com
spec:
rules:
- host: ''
http:
paths:
- path: /
backend:
serviceName: <your_service_name> # Replace it with the name of your target Service.
servicePort: <your_service_port> # Replace it with the port number of your target Service.
Configuring the Returned Status Code for the Permanent Redirection Rule
When configuring a permanent redirection rule, you can use the nginx.ingress.kubernetes.io/permanent-redirect-code annotation to modify its returned status code. For example, to set the status code for the permanent redirection to 308, run the following command:
nginx.ingress.kubernetes.io/permanent-redirect-code: '308'
The configuration in Nginx Ingress is as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-test
namespace: default
annotations:
nginx.ingress.kubernetes.io/permanent-redirect: https://www.example.com
nginx.ingress.kubernetes.io/permanent-redirect-code: '308'
spec:
rules:
- host: ''
http:
paths:
- path: /
backend:
service:
name: <your_service_name> # Replace it with the name of your target Service.
port:
number: <your_service_port> # Replace it with the port number of your target Service.
property:
ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
pathType: ImplementationSpecific
ingressClassName: nginx
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-test
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/permanent-redirect: https://www.example.com
nginx.ingress.kubernetes.io/permanent-redirect-code: '308'
spec:
rules:
- host: ''
http:
paths:
- path: /
backend:
serviceName: <your_service_name> # Replace it with the name of your target Service.
servicePort: <your_service_port> # Replace it with the port number of your target Service.
Configuring a Temporary Redirection Rule
To temporarily redirect an access request to a target website (status code 302), use the nginx.ingress.kubernetes.io/temporal-redirect annotation. For example, to temporarily redirect all access requests to www.example.com, run the following command:
nginx.ingress.kubernetes.io/temporal-redirect: https://www.example.com
The configuration in Nginx Ingress is as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-test
namespace: default
annotations:
nginx.ingress.kubernetes.io/temporal-redirect: https://www.example.com
spec:
rules:
- host: ''
http:
paths:
- path: /
backend:
service:
name: <your_service_name> # Replace it with the name of your target Service.
port:
number: <your_service_port> # Replace it with the port number of your target Service.
property:
ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
pathType: ImplementationSpecific
ingressClassName: nginx
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-test
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/temporal-redirect: https://www.example.com
spec:
rules:
- host: ''
http:
paths:
- path: /
backend:
serviceName: <your_service_name> # Replace it with the name of your target Service.
servicePort: <your_service_port> # Replace it with the port number of your target Service.
Redirecting HTTP to HTTPS
By default, if an ingress uses TLS, requests will be redirected (status code 308) to HTTPS when HTTP is used for access. You can configure the redirection by using the nginx.ingress.kubernetes.io/ssl-redirect annotation.
- If the value of this annotation is set to true, an HTTP access is redirected to HTTPS (status code 308) when the TLS certificate is used.
- If the value of this annotation is set to false, an HTTP access cannot be redirected to HTTPS when the TLS certificate is used.
If you need to forcibly redirect an HTTP access to HTTPS without a TLS, you can configure the redirection by setting the value of nginx.ingress.kubernetes.io/force-ssl-redirect to true.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-test
namespace: default
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
spec:
rules:
- host: ''
http:
paths:
- path: /
backend:
service:
name: <your_service_name> # Replace it with the name of your target Service.
port:
number: <your_service_port> # Replace it with the port number of your target Service.
property:
ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
pathType: ImplementationSpecific
ingressClassName: nginx
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-test
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
spec:
rules:
- host: ''
http:
paths:
- path: /
backend:
serviceName: <your_service_name> # Replace it with the name of your target Service.
servicePort: <your_service_port> # Replace it with the port number of your target Service.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot