How replace_response works
masterThe replace_response module (module name: http.handlers.replace_response) performs substring or regular expression replacements on response bodies.
It operates in two modes:
- Buffer mode (Default): Buffers the entire output to calculate the correct
Content-Lengthheader. This is less memory-efficient but ensures header accuracy. - Streaming mode: Enabled via the
streamoption. This is more memory-efficient but may result in theContent-Lengthheader being removed, as the resulting length is unknown when headers are written to the client.
Important Constraints:
- Compression: The handler cannot perform replacements on compressed content. If your backend (e.g., a reverse proxy) sends compressed responses, you must either decompress them before this handler runs or instruct the backend not to compress by setting the
Accept-Encodingheader toidentity(e.g.,header_up Accept-Encoding identity). - Regex Limits: Regular expression matches longer than 2kb will not be replaced.