nginx入口子路径重定向

use*_*908 6 nginx kubernetes kubernetes-ingress nginx-ingress

我有一个入口控制器和入口资源运行,所有/ devops都映射到后端的devopsservice。当我尝试点击“ http:// hostname / devops ”时,一切正常,我得到了一个页面(尽管没有CSS和样式),其中包含一组超链接,例如其中的一个是“ logs”。

当我单击“日志”超链接时,它会将我重定向到http:// hostname / logs,而我需要它是http:// hostname / devops / logs

知道我能做什么吗?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: my-ingress
 namespace: ingress-nginx
 annotations:
   kubernetes.io/ingress.class: nginx
   nginx.ingress.kubernetes.io/rewrite-target: /
   nginx.ingress.kubernetes.io/add-base-url : "true"
spec:
 rules:
 - host: master1.dev.local
   http:
     paths:
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /devops
Run Code Online (Sandbox Code Playgroud)

Ric*_*ico 6

看起来您的入口没有提供任何服务/devops/*。尝试添加/devops/*具有相同后端的另一条路径。基本上是这样的:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: my-ingress
 namespace: ingress-nginx
 annotations:
   kubernetes.io/ingress.class: nginx
   nginx.ingress.kubernetes.io/rewrite-target: /
   nginx.ingress.kubernetes.io/add-base-url : "true"
spec:
 rules:
 - host: master1.dev.local
   http:
     paths:
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /devops/*
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /devops
Run Code Online (Sandbox Code Playgroud)

更新:以上内容已被弃用,以支持以下内容:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: master1.dev.local
    http:
      paths:
      - backend:
          serviceName: devops1
          servicePort: 10311
        path: /devops(/|$)(.*)
Run Code Online (Sandbox Code Playgroud)

  • `add-base-url` 在 0.22.0 中被[删除](https://github.com/kubernetes/ingress-nginx/pull/3174)。 (2认同)

Kun*_* Li 5

如果您http://hostname/devops/logs直接从浏览器访问 ,您肯定会得到您想要的。但是既然你点击了首页的超链接,那么你只能得到http://hostname/logs,肯定会失败。

因此,您需要/logs在 ingress yaml 中配置后端以对其进行处理,并进行配置nginx.ingress.kubernetes.io/configuration-snippet以确保/logs不会被重写,如下所示:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: my-ingress
 namespace: ingress-nginx
 annotations:
   kubernetes.io/ingress.class: nginx
   nginx.ingress.kubernetes.io/rewrite-target: /
   nginx.ingress.kubernetes.io/add-base-url : "true"
   nginx.ingress.kubernetes.io/configuration-snippet: |
     rewrite ^/logs /logs break;
spec:
 rules:
 - host: master1.dev.local
   http:
     paths:
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /logs
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /devops
Run Code Online (Sandbox Code Playgroud)