trim_types
The `trim_types` directive defines additional MIME types for which whitespace and comments should be stripped from HTML responses.
Description
The trim_types directive in NGINX is part of the ngx_http_trim_filter module, which facilitates the automatic removal of superfluous whitespace and comments from HTML responses. By default, this directive processes only responses with the text/html MIME type. The trim_types directive allows you to extend this functionality by specifying additional MIME types that should also undergo trimming. This is especially useful when serving dynamic content that might use alternative types such as text/xml or other custom MIME types.
To utilize trim_types, you need to provide one or more MIME types as arguments. You can include multiple types, separated by spaces, which instructs the module to apply trimming to each specified MIME type, alongside the standard text/html. The behavior of the directive ensures that content within specific HTML tags like <pre>, <textarea>, <script>, and <style> remains unchanged, preserving necessary formatting and script execution. This helps maintain the integrity of your HTML documents while still optimizing the output by removing unnecessary clutter.
Configurations that require dynamic manipulation of MIME types can leverage variables within the directive. When used in conjunction with other directives such as trim_js and trim_css, trim_types provides extensive control over how content is processed and delivered to users, thereby enhancing page load times and overall performance.
Config Example
http {
server {
location / {
trim_types text/html text/xml;
}
}
}Make sure to list all necessary types; any omitted types will not be trimmed, which can lead to unintended whitespace in the response.
Remember that the trimming is not applied to tags like <pre>, <script>, and <style>. Make sure this behavior fits your needs.
When using variables in the directive, ensure that the variables resolve to valid MIME types. Otherwise, trimming might not behave as expected.