ContentType

Handling Content-Type auto-detection

The Content-Type middleware - or rather its autoDetect option - specifies whether to let the Content-Type header, if it has not been defined by the backend, be automatically set to a value derived from the contents of the response.

As a proxy, the default behavior should be to leave the header alone, regardless of what the backend did with it. However, the historic default was to always auto-detect and set the header if it was not already defined, and altering this behavior would be a breaking change which would impact many users.

This middleware exists to enable the correct behavior until at least the default one can be changed in a future version.

Info

As explained above, for compatibility reasons the default behavior on a router (without this middleware), is still to automatically set the Content-Type header. Therefore, given the default value of the autoDetect option (false), simply enabling this middleware for a router switches the router’s behavior.

The scope of the Content-Type middleware is the MIME type detection done by the core of Traefik (the server part). Therefore, it has no effect against any other Content-Type header modifications (e.g.: in another middleware such as compress).

Configuration Examples

Docker

  1. # Disable auto-detection
  2. labels:
  3. - "traefik.http.middlewares.autodetect.contenttype.autodetect=false"

Kubernetes

  1. # Disable auto-detection
  2. apiVersion: traefik.containo.us/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: autodetect
  6. spec:
  7. contentType:
  8. autoDetect: false

Consul Catalog

  1. # Disable auto-detection
  2. - "traefik.http.middlewares.autodetect.contenttype.autodetect=false"

Marathon

  1. "labels": {
  2. "traefik.http.middlewares.autodetect.contenttype.autodetect": "false"
  3. }

Rancher

  1. # Disable auto-detection
  2. labels:
  3. - "traefik.http.middlewares.autodetect.contenttype.autodetect=false"

File (YAML)

  1. # Disable auto-detection
  2. http:
  3. middlewares:
  4. autodetect:
  5. contentType:
  6. autoDetect: false

File (TOML)

  1. # Disable auto-detection
  2. [http.middlewares]
  3. [http.middlewares.autodetect.contentType]
  4. autoDetect=false

Configuration Options

autoDetect

autoDetect specifies whether to let the Content-Type header, if it has not been set by the backend, be automatically set to a value derived from the contents of the response.