更新时间:2024-12-04 GMT+08:00

Nginx Ingress配置重定向规则

配置永久重定向规则

如果您想将访问请求永久重定向至某个目标网址(状态码为301),您可以通过nginx.ingress.kubernetes.io/permanent-redirect注解进行配置。例如将所有内容永久重定向到www.example.com:

nginx.ingress.kubernetes.io/permanent-redirect: https://www.example.com

在Nginx Ingress中的配置如下:

1.23及以上版本集群
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>  #替换为您的目标服务名称
                port:
                  number: <your_service_port>  #替换为您的目标服务端口
            property:
              ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
            pathType: ImplementationSpecific
  ingressClassName: nginx
1.21及以下版本集群
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>  #替换为您的目标服务名称
              servicePort: <your_service_port>  #替换为您的目标服务端口

配置永久重定向的返回状态码

配置永久重定向时,您可以通过nginx.ingress.kubernetes.io/permanent-redirect-code注解修改永久重定向的返回状态码。例如将永久重定向的状态码设置为308:

nginx.ingress.kubernetes.io/permanent-redirect-code: '308'

在Nginx Ingress中的配置如下:

1.23及以上版本集群
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>  #替换为您的目标服务名称
                port:
                  number: <your_service_port>  #替换为您的目标服务端口
            property:
              ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
            pathType: ImplementationSpecific
  ingressClassName: nginx
1.21及以下版本集群
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>  #替换为您的目标服务名称
              servicePort: <your_service_port>  #替换为您的目标服务端口

配置临时重定向规则

如果您想将访问请求临时重定向至某个目标网址(状态码为302),您可以通过nginx.ingress.kubernetes.io/temporal-redirect注解进行配置。例如将所有内容临时重定向到www.example.com:

nginx.ingress.kubernetes.io/temporal-redirect: https://www.example.com

在Nginx Ingress中的配置如下:

1.23及以上版本集群
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>  #替换为您的目标服务名称
                port:
                  number: <your_service_port>  #替换为您的目标服务端口
            property:
              ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
            pathType: ImplementationSpecific
  ingressClassName: nginx
1.21及以下版本集群
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>  #替换为您的目标服务名称
              servicePort: <your_service_port>  #替换为您的目标服务端口

配置HTTP重定向到HTTPS

默认情况下,若Ingress使用了TLS,通过HTTP访问时,请求将会被重定向(状态码为308)到HTTPS。您可以通过nginx.ingress.kubernetes.io/ssl-redirect注解进行配置,对应的参数值可设置为“true”或“false”:

  • true:使用TLS证书时,将HTTP访问重定向至HTTPS(状态码为308)。
  • false:使用TLS证书时,禁止将HTTP访问重定向至HTTPS。

如果您在没有使用TLS证书的情况下需要强制重定向到HTTPS,您可以使用nginx.ingress.kubernetes.io/force-ssl-redirect: "true"注释实现。

1.23及以上版本集群
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>  #替换为您的目标服务名称
                port:
                  number: <your_service_port>  #替换为您的目标服务端口
            property:
              ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
            pathType: ImplementationSpecific
  ingressClassName: nginx
1.21及以下版本集群
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>  #替换为您的目标服务名称
              servicePort: <your_service_port>  #替换为您的目标服务端口