【问题标题】:Traefik - apply middleware to router except a specific pathTraefik - 将中间件应用于路由器,除了特定路径
【发布时间】:2021-12-07 20:14:55
【问题描述】:

我使用 IP 白名单中间件来过滤我的 Web 应用程序对某些 IPS 的访问,并且它可以正常工作。

但是,我想取消保护特定路径以使其公开(路径为/api/transaction)。

目前,我有(在我的docker-comose.yml):

varnish:
  labels:
    - "traefik.http.routers.api_varnish.rule=Host(`api.local`, `api`)"
    - "traefik.http.routers.api_varnish.tls=true"
    - "traefik.http.routers.api_varnish.middlewares=https-redirect@file"
    - "traefik.http.routers.api_varnish.middlewares=https-whitelist@file"
    - "traefik.http.services.api_varnish.loadbalancer.server.port=80"

这部分有效,然后我补充说:

    # Open middleware for payment IPN calls
   - "traefik.http.routers.api_varnish_transaction.rule=(Host(`api.local`, `api`) && PathPrefix(`/api/transaction`))"
   - "traefik.http.routers.api_varnish_transaction.tls=true"
   - "traefik.http.routers.api_varnish_transaction.priority=2"
   - "traefik.http.routers.api_varnish_transaction.middlewares=https-redirect@file"

我复制了这些行,但我没有将中间件https-whitelist@file 应用到新主机。

它不起作用,我找不到正确的语法或确定我是否可以做到?文档很差。

有什么想法吗?

【问题讨论】:

    标签: traefik


    【解决方案1】:

    有 2 个路由器,1 个用于 /api/transaction,另一个用于 /*,并给第一个路由器更高的优先级(设置更高的数字),例如

    # ...
        labels:
            - traefik.http.routers.router_1.priority=2
    

    现在对 /api/transaction 的请求只会命中 router_1

    https://doc.traefik.io/traefik/routing/routers/#priority

    【讨论】:

    • 这就是我所做的,我更新了我的问题,但仍然应用中间件:(
    • 我在本地对其进行了测试,它对我有用。这是我正在尝试的要点:gist.github.com/kamikazechaser/0dd3dae80c78ac2206d1cc22a12a0c18
    • 感谢您的宝贵时间!关于您的配置,它现在可以工作了!真的很感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2016-05-14
    • 2016-05-31
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    相关资源
    最近更新 更多