Configured Nginx to set caching headers for static assets by adding a location block to match common static file extensions and set an expiration time of 30 days. It should result in faster loading times, reduced bandwidth usage, and a more efficient and responsive user experience. Wdyt @manuhabitela?
25 lines
679 B
Plaintext
25 lines
679 B
Plaintext
server {
|
|
listen 8080;
|
|
server_name localhost;
|
|
|
|
root /usr/share/nginx/html;
|
|
|
|
# Serve static files with caching
|
|
location ~* ^/assets/.*\.(css|js|json|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
|
expires 30d;
|
|
add_header Cache-Control "public, max-age=2592000";
|
|
}
|
|
|
|
# Serve static files
|
|
location / {
|
|
try_files $uri $uri/ /index.html;
|
|
# Add no-cache headers
|
|
add_header Cache-Control "no-cache, no-store, must-revalidate";
|
|
add_header Pragma "no-cache"; # HTTP 1.0 header for backward compatibility
|
|
add_header Expires 0;
|
|
}
|
|
|
|
# Optionally, handle 404 errors by redirecting to index.html
|
|
error_page 404 =200 /index.html;
|
|
}
|