Nginx SPA Single-Page App Fallback & Caching Routing
# Nginx Single Page Application (SPA) Routing & Caching
server {
listen 80;
listen [::]:80;
server_name app.example.com;
root /var/www/app/dist;
index index.html;
# Gzip & Brotli Compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml;
# Immutable static assets (hashed bundles)
location ~* \.(?:css|js|woff2?|svg|png|jpg|webp)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
# SPA Client-Side Routing Fallback
location / {
try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache, must-revalidate";
}
# Security Headers
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
}