简体   繁体   English

Kubernetes Nginx Ingress删除部分URL

[英]Kubernetes Nginx Ingress removing part of URL

I'm deploying a simple app in Kubernetes (on AKS) which is sat behind an Ingress using Nginx, deployed using the Nginx helm chart. 我正在Kubernetes中(在AKS上)部署一个简单的应用程序,该应用程序位于使用Nginx的Ingress后面,并使用Nginx掌舵图进行了部署。 I have a problem that for some reason Nginx doesn't seem to be passing on the full URL to the backend service. 我有一个问题,由于某种原因,Nginx似乎没有将完整的URL传递给后端服务。

For example, my Ingress is setup with the URL of http://app.client.com and a path of /app1g going http://app.client.com/app1 works fine. 例如,我的Ingress是使用http://app.client.com的URL设置的,并且/ app1g的路径为http://app.client.com/app1可以正常工作。 However if I try to go to http://app.client.com/app1/service1 I just end up at http://app.client.com/app1 , it seems to be stripping everything after the path. 但是,如果我尝试转到http://app.client.com/app1/service1,而我最终只访问了http://app.client.com/app1 ,则该路径后似乎剥夺了所有内容。

My Ingress looks like this: 我的入口看起来像这样:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
  creationTimestamp: "2019-04-03T12:44:22Z"
  generation: 1
  labels:
    chart: app-1.1
    component: app
    hostName: app.client.com
    release: app
  name: app-ingress
  namespace: default
  resourceVersion: "1789269"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/app-ingress
  uid: 34bb1a1d-560e-11e9-bd46-9a03420914b9
spec:
  rules:
  - host: app.client.com
    http:
      paths:
      - backend:
          serviceName: app-service
          servicePort: 8080
        path: /app1
  tls:
  - hosts:
    - app.client.com
    secretName: app-prod
status:
  loadBalancer:
    ingress:
    - {}

If I port forward to the service and hit that directly it works. 如果我移植到该服务并直接点击该服务,它将起作用。

So I found the answer to this. 所以我找到了答案。 It seems that as of Nginx v0.22.0 you are required to use capture groups to capture any substrings in the request URI. 从Nginx v0.22.0开始,您似乎需要使用捕获组来捕获请求URI中的任何子字符串。 Prior to 0.22.0 using just nginx.ingress.kubernetes.io/rewrite-target: / worked for any substring. 在0.22.0之前,仅使用nginx.ingress.kubernetes.io/rewrite-target: /用于任何子字符串。 Now it does not. 现在没有。 I needed to ammend my ingress to use this: 我需要修改入口才能使用此功能:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  creationTimestamp: "2019-04-03T12:44:22Z"
  generation: 1
  labels:
    chart: app-1.1
    component: app
    hostName: app.client.com
    release: app
  name: app-ingress
  namespace: default
  resourceVersion: "1789269"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/app-ingress
  uid: 34bb1a1d-560e-11e9-bd46-9a03420914b9
spec:
  rules:
  - host: app.client.com
    http:
      paths:
      - backend:
          serviceName: app-service
          servicePort: 8080
        path: /app1/?(.*)
  tls:
  - hosts:
    - app.client.com
    secretName: app-prod
status:
  loadBalancer:
    ingress:
    - {}

Removing this line should fix your problem: 删除此行应该可以解决您的问题:

nginx.ingress.kubernetes.io/rewrite-target: /

The rewrite target annotation will do exactly what it says: rewrite your request to hit the "/" location. 重写目标注释将完全按照其说明:重写您的命中“ /”位置的请求。 See nginx-ingress docs for rewrite target . 有关重写target,请参见nginx-ingress文档

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

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