Course contentsShow
← Web Development
Lesson 2138 of 2,98239. Network PerformanceFree lesson

Configuring Compression in Web Servers

Learn to enable and configure compression in Nginx, Apache, and Node.js servers with proper settings.

Configuring Compression in Web Servers

You'll learn how to enable and configure compression in Nginx, Apache, and Node.js to deliver smaller files faster.

Now that you understand compression algorithms and pre-compression strategies, it's time to actually implement them in real web servers. Each server platform has its own configuration syntax, but the principles remain the same: tell the server which files to compress, which algorithm to use, and at what level.

Nginx Configuration

Nginx uses the gzip module for dynamic compression:

# In nginx.conf or your site config
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_vary on;

For Brotli, you'll need the ngx_brotli module:

brotli on;
brotli_types text/plain text/css application/json application/javascript;
brotli_comp_level 6;

Apache Configuration

Apache uses mod_deflate for compression:

# In .htaccess or httpd.conf
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css application/json application/javascript
  DeflateCompressionLevel 6
</IfModule>

Node.js with Express

Use the compression middleware:

const express = require('express');
const compression = require('compression');

const app = express();

app.use(compression({
  level: 6,
  threshold: 1000
}));

Each configuration enables compression, specifies which MIME types to compress, sets the compression level (usually 6 is a good balance), and sets a minimum file size threshold to avoid compressing tiny files where overhead outweighs benefits.

Key Takeaway: Enabling compression in web servers requires platform-specific configuration, but all follow the same pattern: specify which content types to compress, choose the compression level, and set appropriate thresholds for optimal performance.