简体   繁体   English

在 GKE 上自动将 HTTP 重定向到 HTTPS

[英]Redirect HTTP to HTTPS automatically on GKE

I used Google-managed SSL certificate on GKE for set up SSL for my website.我在 GKE 上使用 Google 管理的 SSL 证书为我的网站设置 SSL。 This is working example.com (that is insecure) and working also https://example.com(that is secure).这是有效的 example.com(不安全),也有效 https://example.com(安全)。 when i access my website example.com why it's not redirect to https://example.com ?当我访问我的网站 example.com 时,为什么它没有重定向到https://example.com I used below yaml file to set up ssl.我使用下面的 yaml 文件来设置 ssl。

 apiVersion:.networking.gke.io/v1 kind: ManagedCertificate metadata: name: certificate-name spec: domains: - domain-name1 - domain-name2

There is now a supported way of doing this in GKE with Frontend Config: https://cloud.google.com/kube.netes-engine/docs/how-to/ingress-features#https_redirect现在有一种在 GKE 中使用前端配置支持的方法: https://cloud.google.com/kube.netes-engine/docs/how-to/ingress-features#https_redirect

You will need GKE version at least 1.18.10-gke.600 .您至少需要 GKE 版本1.18.10-gke.600

EDIT: As per comment by @mltsy, it also works in 1.17.17 .编辑:根据@mltsy 的评论,它也适用于1.17.17

How we have it set up is:我们如何设置它是:

apiVersion: networking.gke.io/v1beta2
kind: ManagedCertificate
metadata:
  name: <certificate-name>
spec:
  domains:
    - example.com
---
apiVersion: networking.gke.io/v1beta1
kind: FrontendConfig
metadata:
  name: <config-name>
spec:
  redirectToHttps:
    enabled: true
    responseCodeName: MOVED_PERMANENTLY_DEFAULT
---
kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: <ingress-name>
  annotations:
    kubernetes.io/ingress.class: "gce"
    networking.gke.io/managed-certificates: <certificate-name>
    kubernetes.io/ingress.global-static-ip-name: <ip-name>
    networking.gke.io/v1beta1.FrontendConfig: <config-name>
  labels:
    <labels-to-specify-app>
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /*
            backend:
              serviceName: <service-name>
              servicePort: 80

GKE ingress doesn't support HTTP to HTTPS redirect at this time. GKE ingress 目前不支持 HTTP 到 HTTPS 重定向。 You can take a look at this Stack post .你可以看看这篇Stack 帖子 However, you can create a redirection rule for your Ingress resource using setting up an HTTP-to-HTTPS redirect for external HTTP(S) load balancers.但是,您可以使用为外部 HTTP(S) 负载平衡器设置 HTTP 到 HTTPS 重定向来为 Ingress 资源创建重定向规则。

As per document you can disable HTTP by including the kube.netes.io/ingress.allow-http annotation in your Ingress manifest. 根据文档,您可以通过在 Ingress 清单中包含 kube.netes.io/ingress.allow-http 注释来禁用 HTTP。

annotations:
kubernetes.io/ingress.allow-http: "false"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM