Search code examples
azure-service-fabrictraefik

Add custom header to all responses from Traefik on Azure's Service Fabric


I've deployed Traefik on an Azure Service Fabric Cluster. Everything works fine as in Traefik (using SFC's APIs) find other guest services and dynamically create the frontend and backend profiles. So I'm not statically configuring any frontend.

Is there a way to tell Traefik to add a custom header to all responses coming from all the froentends?


Solution

  • Traefik has a custom header feature for matched frontend rules as described here.

    The custom headers are applied per rule...

    Custom headers can be configured through the frontends, to add headers to either requests or responses that match the frontend's rules

    Because you are using dynamic configuration, you will need to add these rules to the services, instead of setting in the traeffik configuration.

    Something like:

    <StatelessServiceType ServiceTypeName="WebServiceType">
      <Extensions>
          <Extension Name="Traefik">
            <Labels xmlns="http://schemas.microsoft.com/2015/03/fabact-no-schema">
              <Label Key="traefik.frontend.rule.example2">PathPrefixStrip: /a/path/to/strip</Label>
              <Label Key="traefik.enable">true</Label>
              <Label Key="traefik.frontend.headers.customRequestHeaders">heaver:value</Label>
            </Labels>
          </Extension>
      </Extensions>
    </StatelessServiceType>
    

    AFAIK, there is no way to apply to all responses in a global way.