7

在 Kubernetes 中使用 NGINX 入口,我看不到将我的流量从非 www 转发到 www 或基于每个主机的另一个域等的方法

我试过查看 configmap 文档,但看不到我需要什么。也许它可以进入入口本身?

我还看到了一个使用注释的示例,但这似乎是入口范围的,所以我不能为每个主机提供特定的重定向

4

3 回答 3

18

确实可以使用简单的注释进行重定向:

但是正如您所提到的,它是“入口”范围内的,并且不能为每个主机、每个域甚至每个路径配置。因此,您必须通过ingress.kubernetes.io/configuration-snippet注释自己完成,这要归功于正则表达式,这为您提供了强大的功能:

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: self-made-redirect
  annotations:
    ingress.kubernetes.io/configuration-snippet: |
      if ($host = 'blog.yourdomain.com') {
        return 301 https://yournewblogurl.com;
      }
      if ($host ~ ^(.+)\.yourdomain\.com$) {
        return 301 https://$1.anotherdomain.com$request_uri;
      }
spec:
  rules:
  - host: ...

如果您不太习惯 NGINX,您将了解更多关于片段中可能发生的事情,尤其是NGINX 文档$host中的变量是什么。

于 2018-11-28T14:40:17.663 回答
3

无论使用 HTTP 或 HTTPS 从 example.com 和www.example.com重定向所有流量到 newdomain.example.com,我最终得到了以下解决方案。

在此示例中,我还使用 cert-manager.io 来请求www.example.com和 example.com 的证书。

重定向是使用注释nginx.ingress.kubernetes.io/permanent-redirect完成的

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: redirect-example-to-newdomain
  annotations:
    kubernetes.io/ingress.class: "nginx"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/permanent-redirect: https://newdomain.example.com
spec:
  tls:
    - hosts:
      - example.com
      - www.example.com
      secretName: example.com-tls
  rules:
    - host: example.com
    - host: www.example.com
于 2021-01-12T15:58:02.883 回答
0

我对注释服务器片段有疑问。更新证书时,进程崩溃。当我删除注释时,更新的证书是成功的。还有其他解决方案吗?例如,在单独的 pod 中创建重定向 301?

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      annotations:
        cert-manager.io/cluster-issuer: letsencrypt-prod
        certmanager.k8s.io/cluster-issuer: letsencrypt-prod
        ingress.kubernetes.io/ssl-redirect: "true"
        meta.helm.sh/release-name: DOMAIN
        meta.helm.sh/release-namespace: DOMAIN
        nginx.ingress.kubernetes.io/configuration-snippet: |
          location ~ ^/online-web {
            return 301 /online;
          }
          if ($host = 'DOMAIN-alias.cz') {
            return 301 https://DOMAIN.cz;
          }
          if ($host ~ ^(.+)\.DOMAIN-alias\.cz$) {
            return 301 https://$1.DOMAIN.cz$request_uri;
          }
          if ($host = 'DOMAIN-alias-2.cz') {
            return 301 https://DOMAIN.cz;
          }
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-buffer-size: 128k
    nginx.ingress.kubernetes.io/server-snippet: |
      if ($host ~ "csas.woltair.cz") {
          return 301 https://woltair.cz/zakaznik/doporuceni;
      }
............
............
  - host: csas.woltair.cz
    http:
      paths:
      - backend:
          service:
            name: woltair-cz-fe
            port:
              number: 8080
        path: /zakaznik/doporuceni
        pathType: ImplementationSpecific
于 2022-02-09T08:31:50.553 回答