|NO.Z.00352|——————————|CloudNative|——|KuberNetes&运维.V68|——|IngressNginx.v04|域名重定向.v02|

一、redirect:重定向
### --- redirect:重定向

~~~     # 地址:
~~~     https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#permanent-redirect
二、为nginx-ingress配置重定向到百度
### --- 创建nginx-ingress.yaml文件;配置annotations

apiVersion: v1
items:
- apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    annotations:
      nginx.ingress.kubernetes.io/permanent-redirect: https://www.baidu.com
    name: ingress-test
    namespace: ratel-test1
  spec:
    rules:
    - host: ingress.test.com
      http:
        paths:
        - backend:
            serviceName: ingress-test
            servicePort: 80
          path: /
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
### --- replace.nginx-ingress-redirect

[root@k8s-master01 IngressNginx]# kubectl replace -f nginx-ingress-redirect-demo.yaml 
ingress.extensions/ingress-test replaced
三、访问域名:http://ingress.test.com/——>会自动跳转到百度
四、进入ingressnginx查看域名重定向配置
### --- 进入容器

[root@k8s-master01 IngressNginx]# kubectl get po -n ingress-nginx -owide
NAME                             READY   STATUS    RESTARTS   AGE     IP             NODE           NOMINATED NODE   READINESS GATES
ingress-nginx-controller-fs68p   1/1     Running   1          5h24m   192.168.1.11   k8s-master01   <none>           <none>
ingress-nginx-controller-mvh2c   1/1     Running   1          8h      192.168.1.14   k8s-node01     <none>           <none>
ingress-nginx-controller-sz8tr   1/1     Running   0          5h24m   192.168.1.15   k8s-node02     <none>           <none>
[root@k8s-master01 IngressNginx]# kubectl exec -ti ingress-nginx-controller-fs68p -n ingress-nginx -- bash
bash-5.0$ ls
nginx.conf
~~~     # 进入容器查看ingress.test.com

bash-5.0$ grep "ingress.test.com" nginx.conf
    ## start server ingress.test.com      // 域名的开始
        server_name ingress.test.com ;    // 配置项
    ## end server ingress.test.com        // 域名的结束
### --- 查看域名配置内容
~~~     注:它自动生成了这个配置,把这个根跳转到www.baidu.com 
~~~     注:若是nginx配置,需要自己手动添加的
~~~     注:若是使用ingress使用自动声明的方式,就会自动添加,直接使用annotations声明就可以

bash-5.0$ grep "## start server ingress.test.com" nginx.conf -A 200
            # In case of errors try the next upstream server before returning an error
            proxy_next_upstream                     error timeout;
            proxy_next_upstream_timeout             0;
            proxy_next_upstream_tries               3;
            
            return 301 https://www.baidu.com;
            
            proxy_pass http://upstream_balancer;
            
            proxy_redirect                          off;
            
        }
五、也可以更改状态码:默认是301,更改为308
### --- 创建ingress-test1的更改状态码yaml

[root@k8s-master01 IngressNginx]# vim nginx-ingress-redirect-308-demo.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/permanent-redirect: https://www.baidu.com
    nginx.ingress.kubernetes.io/permanent-redirect-code: "308"
  name: ingress-test
  namespace: ratel-test1
spec:
  rules:
  - host: ingress.test.com
    http:
      paths:
      - backend:
          serviceName: ingress-test
          servicePort: 80
        path: /
        pathType: ImplementationSpecific
### --- replace-nginx-ingress-308

[root@k8s-master01 IngressNginx]# kubectl replace -f nginx-ingress-redirect-308-demo.yaml 
### --- 查看百度的状态码是否为308

[root@k8s-master01 IngressNginx]# kubectl exec -ti ingress-nginx-controller-fs68p -n ingress-nginx -- bash
bash-5.0$ grep "https://www.baidu.com" nginx.conf
            return 308 https://www.baidu.com;

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(19)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示