Facebook Pixel

Nginx Performance Updates and Optimization Gzip, Caching, and Worker Tuning

Learn how to update Nginx for better performance gzip compression, static file caching, worker process tuning, and proxy buffer optimization.

Nginx Performance Updates and Optimization

This guide covers performance updates to make your Nginx + Node.js deployment faster.

1. Update Worker Processes

# /etc/nginx/nginx.conf worker_processes auto; # One per CPU core worker_connections 2048; # Connections per worker worker_rlimit_nofile 65535; # File descriptor limit

2. Enable Gzip Compression

# In http block or server block gzip on; gzip_vary on; gzip_min_length 1000; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml application/xml+rss application/x-javascript;

This reduces response sizes by 60-80% for text-based content.

3. Cache Static Files

server { # ... # Cache static assets for 1 year location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { expires 1y; add_header Cache-Control "public, immutable"; access_log off; } }

React build files have hashed names (main.a1b2c3d4.js), so aggressive caching is safe.

4. Proxy Buffering

location /api/ { proxy_pass http://localhost:3000; # Buffer settings proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; proxy_busy_buffers_size 8k; # ... other headers ... }

Buffering lets Nginx collect the full response from Node.js before sending to the client, freeing Node.js to handle other requests.

5. Keep-Alive Connections

# In http block keepalive_timeout 65; keepalive_requests 100; # Upstream keep-alive (to Node.js) upstream nodejs_backend { server 127.0.0.1:3000; keepalive 32; } server { location / { proxy_pass http://nodejs_backend; proxy_http_version 1.1; proxy_set_header Connection ""; } }

6. HTTP/2

server { listen 443 ssl http2; # Enable HTTP/2 # ... SSL config ... }

HTTP/2 allows multiplexing (multiple requests over one connection) and is faster than HTTP/1.1.

7. Brotli Compression (Better than Gzip)

sudo apt install -y libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static
brotli on; brotli_comp_level 6; brotli_types text/plain text/css application/json application/javascript text/xml;

Brotli is ~15-20% better than gzip for text content.

8. Open File Cache

# In http block open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;

This caches file metadata, reducing disk I/O for frequently accessed files.

9. Update Timeouts

# Client timeouts (prevent slowloris) client_body_timeout 10; client_header_timeout 10; keepalive_timeout 65; send_timeout 10; # Proxy timeouts location /api/ { proxy_connect_timeout 5s; proxy_send_timeout 30s; proxy_read_timeout 30s; }

10. Disable Access Log for Static Files

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ { access_log off; expires 1y; }

This reduces disk I/O and log file size.

Applying Performance Updates

# 1. Update config sudo nano /etc/nginx/nginx.conf sudo nano /etc/nginx/sites-available/devtinder # 2. Test sudo nginx -t # 3. Reload (zero downtime) sudo systemctl reload nginx # 4. Verify with performance test # Use Google PageSpeed Insights: https://pagespeed.web.dev # Or: curl -I -H "Accept-Encoding: gzip" https://yourdomain.com

The Takeaway

Nginx performance updates include: worker_processes auto with 2048 connections, gzip compression (level 6, text types), static file caching (1 year with immutable), proxy buffering (frees Node.js), keep-alive connections (65s timeout, 32 upstream), HTTP/2 for multiplexing, Brotli compression (better than gzip), open file cache (reduces disk I/O), appropriate timeouts (prevent slowloris), and access_log off for static files. Test with nginx -t and reload.

Enable gzip compression (level 6, text types), cache static files for 1 year with immutable, use proxy_buffering to free Node.js, enable keep-alive connections (65s timeout, 32 upstream), use HTTP/2, set worker_processes auto with 2048 connections, and disable access logging for static files.

Brotli is a compression algorithm that's ~15-20% better than gzip for text content. Install libnginx-mod-http-brotli-filter, enable with brotli on and brotli_comp_level 6. Use it alongside or instead of gzip for better compression.

Add a location block matching static file extensions (js, css, png, jpg, etc.) with expires 1y, add_header Cache-Control 'public, immutable', and access_log off. React build files have hashed names so aggressive caching is safe new versions get new URLs.

proxy_buffering on makes Nginx collect the full response from Node.js before sending to the client. This frees Node.js to handle other requests instead of streaming slowly to the client. Set proxy_buffer_size 4k and proxy_buffers 8 4k for typical API responses.

Change listen 443 ssl to listen 443 ssl http2 in the server block. HTTP/2 allows multiplexing (multiple requests over one connection), header compression, and is faster than HTTP/1.1. It requires SSL (HTTPS). Test with curl -I --http2 https://yourdomain.com.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.