The Compression plugin provides automatic response compression using gzip, deflate, or Brotli algorithms. It reduces bandwidth usage and improves page load times by compressing text-based responses before sending them to clients.
The compression plugin intercepts HTTP responses and compresses them based on the client's Accept-Encoding header. It intelligently decides whether to compress based on content type, size thresholds, and client capabilities. This plugin is essential for optimizing web performance and reducing bandwidth costs.
The compression plugin is configured as part of the plugin pipeline in your host configuration:
<li itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/Plugin">
<span itemprop="library">file://./plugins/compression.so</span>
<meta itemprop="algorithms" content="gzip,brotli">
<meta itemprop="min_size" content="1024">
<meta itemprop="compression_level" content="6">
</li>
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
algorithms |
String (comma-separated) | No | "gzip,deflate" | Enabled compression algorithms: "gzip", "deflate", "brotli" (or "br") |
min_size |
Integer | No | 1024 | Minimum response size in bytes to compress (default: 1KB) |
max_size |
Integer | No | 10485760 | Maximum response size in bytes to compress (default: 10MB) |
compression_level |
Integer (0-9) | No | 6 | Compression level: 0 (no compression) to 9 (maximum compression) |
compressible_types |
String (comma-separated) | No | See defaults | MIME types to compress |
By default, the plugin compresses these content types:
text/html
- HTML documentstext/css
- Stylesheetstext/javascript
- JavaScript filestext/plain
- Plain text filesapplication/json
- JSON dataapplication/javascript
- JavaScript filesapplication/xml
- XML documentsapplication/rss+xml
- RSS feedsapplication/atom+xml
- Atom feedsimage/svg+xml
- SVG imagesTypical pipeline order:
1. basic-auth.so → Authenticates user
2. authorization.so → Checks permissions
3. selector-handler.so → Processes requests
4. file-handler.so → Serves files
5. compression.so → Compresses responses ✓
6. access-log.so → Logs requests
<li itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/Plugin">
<span itemprop="library">file://./plugins/compression.so</span>
</li>
<li itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/Plugin">
<span itemprop="library">file://./plugins/compression.so</span>
<meta itemprop="algorithms" content="brotli,gzip">
<meta itemprop="compression_level" content="4">
<meta itemprop="min_size" content="512">
</li>
# Request with gzip support
curl -H "Accept-Encoding: gzip" -I http://localhost:3000/large-file.html
# Request with Brotli support
curl -H "Accept-Encoding: br" -I http://localhost:3000/large-file.html
# View compression headers
curl -H "Accept-Encoding: gzip, br" -v http://localhost:3000/index.html 2>&1 | grep -i encoding
// Modern browsers automatically handle compression
fetch('http://localhost:3000/api/data.json')
.then(response => {
// Response is automatically decompressed
console.log('Content-Encoding:', response.headers.get('Content-Encoding'));
return response.json();
})
.then(data => console.log(data));
Level | Compression Ratio | CPU Usage | Use Case |
---|---|---|---|
1-3 | Low | Very Low | High-traffic sites with CPU constraints |
4-6 | Medium | Medium | Balanced performance (recommended) |
7-9 | High | High | Maximum compression, low-traffic sites |
Algorithm | Chrome | Firefox | Safari | Edge | IE11 |
---|---|---|---|---|---|
gzip | ✓ | ✓ | ✓ | ✓ | ✓ |
deflate | ✓ | ✓ | ✓ | ✓ | ✓ |
brotli | ✓ (HTTPS) | ✓ (HTTPS) | ✓ | ✓ (HTTPS) | ✗ |
The plugin logs compression statistics when running with -v
:
[Compression] Compressed 45632 bytes to 8921 bytes using gzip (80.5% reduction)
Monitor compression effectiveness:
Vary: Accept-Encoding
for proper cachingIssue | Cause | Solution |
---|---|---|
No compression occurring | Content type not in list or size thresholds | Check content type and size configuration |
Already compressed warning | Content has Content-Encoding header | Normal - plugin correctly skipping compressed content |
High CPU usage | Compression level too high | Reduce compression_level to 4-6 |
Brotli not working | HTTP instead of HTTPS | Most browsers require HTTPS for Brotli |
Memory issues | Compressing very large files | Reduce max_size limit |
Run the server with -v
flag to see compression details:
./rusty-beam -v config.html