Fast page load times and HTTPS encryption are foundational requirements for modern search visibility and reader trust. Even on an affordable 1–2 GB RAM cloud VPS, properly tuned Nginx configuration delivers sub-second responses without needing heavy server hardware.
This tutorial explains how to configure Gzip compression, browser caching headers, and automated Let’s Encrypt TLS certificates cleanly on Ubuntu 22.04 or 24.04.
Step 1: Verify Your Nginx Virtual Host Structure
Ubuntu stores site configurations inside /etc/nginx/sites-available/ and activates them via symlinks in /etc/nginx/sites-enabled/. Inspect your site configuration file first:
sudo nano /etc/nginx/sites-available/yourdomain.com
Make sure unused default server blocks do not conflict with your domain:
ls -la /etc/nginx/sites-enabled/
Step 2: Enable Gzip Compression for Text Assets
HTML, CSS, JavaScript, JSON, and SVG files shrink by 60–80% when compressed before transfer. Inside your http block in /etc/nginx/nginx.conf (or inside your site’s server block), include clean Gzip directives:
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types
text/plain
text/css
application/json
application/javascript
text/xml
application/xml
application/xml+rss
image/svg+xml;
Setting gzip_comp_level 5 offers near-maximum compression ratio without wasting CPU cycles on small VPS instances. Avoid compressing already-compressed binary formats like JPEG, PNG, WebP, or WOFF2.
Step 3: Add Browser Cache Headers for Static Files
Without explicit cache headers, browsers re-download stylesheets, icons, and fonts on every page click. Add a dedicated location block inside your server configuration:
location ~* \.(js|css|png|jpg|jpeg|gif|webp|svg|ico|woff2?)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
access_log off;
}
Disabling access_log for static assets also reduces disk I/O and keeps Nginx logs clean for actual page requests and errors.
Step 4: Block Common Attack & Hidden File Paths
Protect hidden files (such as .git or .htaccess) and disable noisy WordPress XML-RPC brute-force vectors if you do not use legacy remote publishing clients:
location ~ /\. {
deny all;
}
location = /xmlrpc.php {
deny all;
}
Step 5: Issue and Verify Free Let’s Encrypt HTTPS SSL
Before running Certbot, confirm your domain’s A record points to your VPS IP and ports 80 and 443 are open. Install Certbot with the Nginx plugin:
sudo apt update
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com --redirect
Certbot automatically configures TLS certificates, sets up HTTP-to-HTTPS 301 redirects, and registers a systemd timer for automatic renewal every 60 days.
Test Automatic SSL Renewal
Never assume SSL renewals will work later—test the renewal pipeline right after setup:
sudo certbot renew --dry-run
If you see Congratulations, all simulated renewals succeeded, your certificates will renew automatically without downtime.
Step 6: Verify Headers and Compression from Terminal
Before reloading Nginx, always test syntax:
sudo nginx -t && sudo systemctl reload nginx
Then inspect your live response headers using curl:
curl -I -H "Accept-Encoding: gzip" https://yourdomain.com/
Confirm the output shows HTTP/2 200 (or HTTP/1.1 200 OK) and Content-Encoding: gzip on HTML/CSS assets.
Common Troubleshooting Notes
- Certbot fails with NXDOMAIN: Wait for DNS propagation or check that both the root domain (
@) andwwwhave validArecords pointing to the server IP. - Redirect Loop with Cloudflare: If you place Cloudflare proxy in front of Nginx after Certbot finishes, set Cloudflare’s SSL/TLS encryption mode to Full (strict) rather than Flexible.