您可以使用 k8s Ingress 将 HTTP 重定向到 HTTPS 吗?

Can you redirect HTTP to HTTPS with a k8s Ingress?

我已经尝试了关于堆栈溢出的所有方法,但找不到可以将 http 重定向到 https 的解决方案。我当前的配置如下。

我的入口是:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/ingress.global-static-ip-name: my-address
    networking.gke.io/managed-certificates: my-certificate
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
spec:
  rules:
  - host: mydomain.com
    http:
      paths:
      - path: /*
        pathType: ImplementationSpecific
        backend:
          service:
            name: my-service
            port:
              number: 2400

我的服务是:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: nodeweb
  ports:
  - name: my-service-port
    protocol: TCP
    port: 2400
    targetPort: 2400

对于 GKE (1.17.13-gke.2600+),使用 networking.gke.io/v1beta1.FrontendConfig 注释找到 this document explaining how to configure FrontendConfig with http-to-https redirect. Then you associate 带有 Ingress 的 FrontendConfig。