使用正则表达式时Traefik的路由规则

时间:2019-07-19 20:37:50

标签: azure azure-service-fabric traefik

使用Traefik 1.7.12版部署到Azure Service Fabric。

我希望Traefik将缓存控制标头添加到来自Web应用程序的某些请求的响应,该Web应用程序也已部署到集群。我发现可以完成此操作的方法是为我的应用程序创建2个具有不同前端路由规则的单独服务。最终结果将是这样的:

Route Rule
Host:devmachine.localhost;PathPrefix:/

其中添加了“ cache-control:no-cache”响应标头。

AND

Route Rule
Host:devmachine.localhost;Path:/{url:(.*)(/banner$|\.(js|css)\??)}

其中添加了“ cache-control:max-age:86400”响应标头。

以下分别是集群中每个服务清单的摘要:

      <Extensions>
        <Extension Name="Traefik">
          <Labels xmlns="http://schemas.microsoft.com/2015/03/fabact-no-schema">
            <Label Key="traefik.enable">true</Label>
            <Label Key="traefik.frontend.passHostHeader">true</Label>
            <Label Key="traefik.frontend.rule">Host:devmachine.localhost;PathPrefix:/</Label>
            <Label Key="traefik.backend.loadbalancer.stickiness">true</Label>
            <Label Key="traefik.backend.healthcheck.path">/somepage</Label>
            <Label Key="traefik.backend.healthcheck.interval">60s</Label>
            <label key="traefik.frontend.headers.customResponseHeaders">cache-control:no-cache</label>
          </Labels>
        </Extension>
      </Extensions>

AND

      <Extensions>
        <Extension Name="Traefik">
          <Labels xmlns="http://schemas.microsoft.com/2015/03/fabact-no-schema">
            <Label Key="traefik.enable">true</Label>
            <Label Key="traefik.frontend.passHostHeader">true</Label>
            <Label Key="traefik.frontend.rule">Host:devmachine.localhost;Path:/{url:(.*)(/banner$|\\.(js|css)\\??)}</Label>
            <label key="traefik.frontend.headers.customResponseHeaders">cache-control:max-age=86400</label>
            <label key="traefik.frontend.priority">200</label>
          </Labels>
        </Extension>
      </Extensions>

问题是我所有的响应都得到“ no-cache”报头,好像没有应用第二个匹配器,包括以下示例URI(应该由第二个路由规则处理):

https://example.com/banner
https://example.com/scripts/some.js
https://example.com/scripts/some-hdbxy778.js
https://example.com/scripts/someother.js?v=2.744

尽管第2条路由规则较长(因为traefik应该按长度降序对它们进行排序),并且手动设置了优先级(200),但事实如此。

1 个答案:

答案 0 :(得分:0)

事实证明这是套管问题。 traefik中的Service Fabric提供程序使用“ xml.Unmarshal”将扩展配置反序列化为本地类型。

根据文档(https://golang.org/pkg/encoding/xml/#Unmarshal

  

Unmarshal使用区分大小写的比较来匹配XML元素名称   标记值和结构字段名称。

有道理。

Traefik的文档已更新为指出:https://github.com/containous/traefik/pull/5160