前言

由于本站主域名为blog.wanderto.top,但是又希望wanderto.top也能访问本站。多个域名指向网站不利于SEO,所以采用重定向的方式。本篇文章主要记录两种k8s中Nginx Ingress Controller重定向域名的方法

Permanent Redirect

redirect主要用于域名重定向,比如访问a.com被重定向到b.com
如下我们配置访问ng.coolops.com重定向到www.baidu.com
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/permanent-redirect: "https://www.baidu.com"
spec:
  rules:
  - host: ng.coolops.cn 
    http:
      paths:
      - path:
        backend:
          serviceName: ng-svc
          servicePort: 80
以上参考自:https://cloud.tencent.com/developer/article/1628571

按以上这种方式尝试使用nginx.ingress.kubernetes.io/permanent-redirect但是没生效,不知道为什么,我的版本是最新的,应该支持才是。而且我在官方文档也没有看到这种方式

Snippet

官方文档中有另一种可行的方式,同样是在Ingress中添加一个annotation,不过会复杂点,参考:snippet
  annotations:
    nginx.org/server-snippets: |
      if ($host = 'old.example.com') {
        return 301 https://new..example.com;
      }
通过使用脚本的方式重定向,不过默认是关闭这个功能的,需要开启,参考:相关文档

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注