Traefik在使用regex时的路由规则
Traefik's routing rule when using regex
使用部署到 Azure Service Fabric 的 Traefik 版本 1.7.12。
我希望 Traefik 将 cache-control header 添加到来自也部署到集群的 Web 应用程序的某些请求的响应中。我发现可以做到这一点的方法是为我的应用程序创建 2 个具有不同前端路由规则的独立服务。最终结果将是这样的:
Route Rule
Host:devmachine.localhost;PathPrefix:/
添加 "cache-control:no-cache" 响应 header。
和
Route Rule
Host:devmachine.localhost;Path:/{url:(.*)(/banner$|\.(js|css)\??)}
添加 "cache-control:max-age:86400" 响应 header。
这里分别是集群中每个服务清单的片段:
<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>
和
<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" header,就好像没有应用第二个匹配器一样,包括以下示例 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
尽管第二条路由规则更长(因为 traefik 应该按长度降序对它们进行排序)并且手动设置了优先级 (200)。
原来是大小写问题。 Traefik 中的 Service Fabric 提供程序利用 "xml.Unmarshal".
将扩展配置反序列化为本地类型
根据文档 (https://golang.org/pkg/encoding/xml/#Unmarshal)
Unmarshal uses a case-sensitive comparison to match XML element names
to tag values and struct field names.
有道理。
Traefik 的文档已更新以指出这一点:https://github.com/containous/traefik/pull/5160
使用部署到 Azure Service Fabric 的 Traefik 版本 1.7.12。
我希望 Traefik 将 cache-control header 添加到来自也部署到集群的 Web 应用程序的某些请求的响应中。我发现可以做到这一点的方法是为我的应用程序创建 2 个具有不同前端路由规则的独立服务。最终结果将是这样的:
Route Rule
Host:devmachine.localhost;PathPrefix:/
添加 "cache-control:no-cache" 响应 header。
和
Route Rule
Host:devmachine.localhost;Path:/{url:(.*)(/banner$|\.(js|css)\??)}
添加 "cache-control:max-age:86400" 响应 header。
这里分别是集群中每个服务清单的片段:
<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>
和
<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" header,就好像没有应用第二个匹配器一样,包括以下示例 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
尽管第二条路由规则更长(因为 traefik 应该按长度降序对它们进行排序)并且手动设置了优先级 (200)。
原来是大小写问题。 Traefik 中的 Service Fabric 提供程序利用 "xml.Unmarshal".
将扩展配置反序列化为本地类型根据文档 (https://golang.org/pkg/encoding/xml/#Unmarshal)
Unmarshal uses a case-sensitive comparison to match XML element names to tag values and struct field names.
有道理。
Traefik 的文档已更新以指出这一点:https://github.com/containous/traefik/pull/5160