简体   繁体   English

Nginx 入口路径未指向服务/路径

[英]Nginx Ingress path is not pointing to service / path

I have nginx ingress installed on my cluster.我的集群上安装了 nginx ingress。 Here is the yaml这是yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-client
  annotations:
    kubernetes.io/ingress.class: nginx
  namespace: dev
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: client-service
            port: 
              number: 80
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: api-service
            port: 
              number: 80

When I hit / prefix it is good.当我点击 / 前缀时,它很好。 curl http://example.com (All Good) curl http://example.com (一切正常)

Problem:问题:

But when I hit / api prefix, it returns /api of the service not / of the service但是当我点击 /api 前缀时,它返回服务的 /api 而不是服务的 /

curl http://example.com/api (It should link to api-service , but it is linking to api-service/api) curl http://example.com/api (它应该链接到 api-service ,但它链接到 api-service/api )

Any help will be appreciated!任何帮助将不胜感激!

This is because a path / with type Prefix will match / and everything after, including /api .这是因为带有类型Prefix的路径/将匹配/之后的所有内容,包括/api So your first rule overshadows the second rule in some sense.所以你的第一条规则在某种意义上掩盖了第二条规则。


I don't know if it's an option for you, but it would be probably most elegant and idiomatic to use different hostnames for both services.我不知道这是否适合您,但为两种服务使用不同的主机名可能是最优雅和惯用的。 If you deploy cert-manager , this shouldn't be a problem.如果您部署cert-manager ,这应该不是问题。

rules:
- host: example.com
  http:
    paths:
    - path: /
      pathType: Prefix
      backend:
        service:
          name: client-service
          port: 
            number: 80
# use a different hostname for the api
- host: api.example.com
  http:
    paths:
    - path: /
      pathType: Prefix
      backend:
        service:
          name: api-service
          port: 
            number: 80

Another option could be to use regex in your frontend path rule.另一种选择是在前端路径规则中使用正则表达式 And let it not match when the slash is followed by api. For that, you need to set an annotation.并在斜杠后跟api时让它不匹配。为此,您需要设置一个注释。

annotations:
  nginx.ingress.kubernetes.io/use-regex: "true"

Then you can do something like the below for your frontend service.然后你可以为你的前端服务做类似下面的事情。 Using a negative lookahead .使用负前瞻

- path: /(?!api).*

Alternatively, but less pretty, you could add a path prefix to your frontend service and strip it away via path rewrite annotation.或者,但不太漂亮,您可以向前端服务添加路径前缀并通过路径重写注释将其删除。 But then you may have to write two separate ingress manifests as this is annotation counts for both, or you need to use a more complex path rewrite rule.但是你可能必须编写两个单独的入口清单,因为这是两者的注释计数,或者你需要使用更复杂的路径重写规则。

annotations:
  nginx.ingress.kubernetes.io/rewrite-target: /$2
- path: /ui(/|$)(.*)

Perhaps it's also sufficient to move the more specific route, /api , before the generic / route.也许在通用/路由之前移动更具体的路由/api就足够了。 In that case, switch the path around in the list.在这种情况下,请在列表中切换路径。 If they end up in that order in the nginx config, nginx should be able to handle it as desired.如果它们在 nginx 配置中以该顺序结束,则 nginx 应该能够根据需要处理它。

You could use nginx.ingress.kube.netes.io/rewrite-target :您可以使用nginx.ingress.kube.netes.io/rewrite-target

In some scenarios the exposed URL in the backend service differs from the specified path in the Ingress rule.在某些场景下,后端服务中暴露的URL与Ingress规则中指定的路径不同。 Without a rewrite any request will return 404. Set the annotation nginx.ingress.kube.netes.io/rewrite-target to the path expected by the service.如果没有重写,任何请求都将返回 404。将注释 nginx.ingress.kube.netes.io/rewrite-target 设置为服务期望的路径。

So, here you could change your ingress as next:所以,在这里你可以改变你的入口如下:

metadata:
  name: ingress-client
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
  namespace: dev

The ingress definition above will result in the following rewrites:上面的入口定义将导致以下重写:

  • api-service/api rewrites to api-service/ api-service/api重写为api-service/

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

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