| outline |
|
||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| title | Response Compression | ||||||||||||||||||||||||||||
| titleTemplate | NpgsqlRest | ||||||||||||||||||||||||||||
| description | Configure HTTP response compression in NpgsqlRest. Enable Brotli and Gzip compression, control compression levels, and specify MIME types to compress. | ||||||||||||||||||||||||||||
| head |
|
Response compression settings for reducing HTTP response sizes using Brotli and Gzip algorithms.
{
"ResponseCompression": {
"Enabled": false,
"EnableForHttps": false,
"UseBrotli": true,
"UseGzipFallback": true,
"CompressionLevel": "Optimal",
"IncludeMimeTypes": [
"text/plain",
"text/css",
"application/javascript",
"text/html",
"application/xml",
"text/xml",
"application/json",
"text/json",
"image/svg+xml",
"font/woff",
"font/woff2",
"application/font-woff",
"application/font-woff2"
],
"ExcludeMimeTypes": []
}
}| Setting | Type | Default | Description |
|---|---|---|---|
Enabled |
bool | false |
Enable response compression for HTTP responses. |
EnableForHttps |
bool | false |
Enable response compression for HTTPS responses. |
UseBrotli |
bool | true |
Use Brotli compression algorithm when supported by client. |
UseGzipFallback |
bool | true |
Use Gzip compression as fallback when Brotli is not supported. |
CompressionLevel |
string | "Optimal" |
Compression level: Optimal, Fastest, NoCompression, SmallestSize. |
IncludeMimeTypes |
array | (see below) | MIME types to include for compression. |
ExcludeMimeTypes |
array | [] |
MIME types to exclude from compression. |
| Level | Description |
|---|---|
Optimal |
Balance between compression ratio and speed (default). |
Fastest |
Fastest compression with lower compression ratio. |
SmallestSize |
Best compression ratio but slower. |
NoCompression |
No compression applied. |
Brotli provides better compression ratios than Gzip, especially for text content. When UseBrotli is true, the server will use Brotli compression if the client supports it (indicated by Accept-Encoding: br header).
When UseGzipFallback is true, the server falls back to Gzip compression for clients that don't support Brotli but do support Gzip (indicated by Accept-Encoding: gzip header).
::: warning Security Consideration
Enabling compression for HTTPS responses (EnableForHttps: true) may expose your application to BREACH-style attacks. Only enable if you understand the security implications and have appropriate mitigations in place.
:::
The default IncludeMimeTypes covers common compressible content:
| Category | MIME Types |
|---|---|
| Text | text/plain, text/css, text/html |
| JavaScript | application/javascript |
| XML | application/xml, text/xml |
| JSON | application/json, text/json |
| SVG | image/svg+xml |
| Fonts | font/woff, font/woff2, application/font-woff, application/font-woff2 |
Enable compression for production:
{
"ResponseCompression": {
"Enabled": true,
"EnableForHttps": true,
"UseBrotli": true,
"UseGzipFallback": true,
"CompressionLevel": "Optimal"
}
}High-compression configuration for bandwidth-constrained environments:
{
"ResponseCompression": {
"Enabled": true,
"EnableForHttps": true,
"UseBrotli": true,
"UseGzipFallback": true,
"CompressionLevel": "SmallestSize"
}
}- Comment Annotations Guide - How annotations work
- Configuration Guide - How configuration works
- Server & SSL - Configure HTTPS and Kestrel web server
- Logging - Configure logging outputs