【问题标题】:why the traefik did not match the url by the path为什么 traefik 与路径中的 url 不匹配
【发布时间】:2021-09-26 10:48:41
【问题描述】:

我使用 traefik 2.2.1 作为我的 kubernetes 入口控制器。现在我有一个 url 需要转发,这是我现在的 traefik 配置:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  creationTimestamp: '2021-09-26T09:10:02Z'
  generation: 4
  name: uat-zhuolian-manage-route
  namespace: dabai-uat
  resourceVersion: '106700291'
  selfLink: >-
    /apis/traefik.containo.us/v1alpha1/namespaces/dabai-uat/ingressroutes/uat-zhuolian-manage-route
  uid: d0b82fab-cd6a-49f0-96dc-f6c0f381bcc5
spec:
  entryPoints:
    - web
  routes:
    - kind: Rule
      match: Host(`zhuolian-pro-manage.example.com`)
      services:
        - name: be-zhuolian-frontend
          port: 80
    - kind: Rule
      match: Host(`zhuolian-pro-manage.example.com`) && Path(`/service`)
      services:
        - name: soa-zhuolian-service
          port: 11032

我希望 traefik 做的是默认将 example.com 转发到 be-zhuolian-frontend。如果请求url中包含service,例如url为xxx.example.com/service/foo/bar,则转发到后端服务soa-zhuolian-service。但现在当我从远程云主机请求 url 时:

curl --header 'Host:zhuolian-pro-manage.example.com' http://172.19.104.230/service/zhuolian/report/user/captcha -L

显示错误:

[root@fat001 ~]# curl --header 'Host:zhuolian-pro-manage.example.com' http://172.19.104.230/service/zhuolian/report/user/captcha -L
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.19.0</center>
</body>
</html>

带有service关键字的url似乎仍然路由到前端服务,我应该如何解决这个问题并使service成功匹配后端服务?

【问题讨论】:

    标签: kubernetes traefik


    【解决方案1】:

    使用PathPrefix 而不是path,来自官方文档:

    如果您的服务仅侦听确切的路径,请使用 Path。例如, 路径:/products 将匹配 /products 但不匹配 /products/shoes。

    如果您的服务侦听特定的基础,请使用 前缀 匹配器 路径,但也为子路径上的请求提供服务。例如,路径前缀: /products 将匹配 /products 但也匹配 /products/shoes 和 /产品/衬衫。由于路径按原样转发,因此您的服务是 预计会在 /products 上收听。

    所以像这样调整配置:

    spec:
      entryPoints:
        - web
      routes:
        - kind: Rule
          match: Host(`zhuolian-pro-manage.example.com`) && PathPrefix(`/service`)
          middlewares:
            - name: service-stripprefix
          services:
            - name: soa-zhuolian-service
              port: 11032
        - kind: Rule
          match: Host(`zhuolian-pro-manage.example.com`)
          services:
            - name: be-zhuolian-frontend
              port: 80
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 2021-03-29
      相关资源
      最近更新 更多