Fix PageSpeed Score on Nginx
Last updated: April 2026
The most impactful Nginx config changes for Google PageSpeed score. Each section targets a specific PSI audit.
Get your personalised PageSpeed fix โCache-Control for static assets
location ~* \.(js|css|woff2|woff)$ {
expires 1y;
add_header Cache-Control "public, max-age=31536000, immutable" always;
}
location ~* \.(png|jpg|jpeg|webp|svg|ico)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000" always;
}
Enable gzip and brotli compression
gzip on;
gzip_vary on;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript
text/xml application/xml image/svg+xml font/woff2;
# Brotli (requires ngx_brotli module)
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/json application/javascript
image/svg+xml font/woff2;
Enable HTTP/2
server {
listen 443 ssl http2;
server_name yoursite.com;
}
Preload LCP image with Link header
location = / {
add_header Link "</hero.webp>; rel=preload; as=image; type=image/webp" always;
}
Reduce TTFB with proxy caching
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=main:10m;
location / {
proxy_cache main;
proxy_cache_valid 200 60s;
proxy_cache_use_stale updating;
proxy_cache_background_update on;
proxy_pass http://backend;
}