Skip to content

inject

inject rewrites response bodies after the next middleware returns. It matches requests by path using doublestar patterns.

yaml
server:
  http:
    middlewares:
      inject_html:
        inject:
          path_map:
            "**/*.html":
              - old: "</head>"
                new: |
                  <script src="/runtime.js"></script>
                  </head>

Replacement Fields

FieldDescription
regexRegular expression to replace. When set, it overrides old.
oldLiteral string to replace.
newReplacement string.
add_prefixString inserted at the beginning of the response body.
add_postfixString appended to the response body.
valueName of a loaded map[string]any. Each key is replaced with its value.
delayOptional duration to sleep before applying this replacement.

inject can read and rewrite gzip responses when the upstream response has Content-Encoding: gzip; it recompresses the body before returning it.