HTML pages referred static files (images, js and css etc) can be cached in your browser by setting HTTP response header attributes about cache.
Two main types of cache headers, cache-control and expires, define the caching characteristics for your resources. Typically, cache-control is considered a more modern and flexible approach than expires, but both headers can be used simultaneously.
Cache headers are applied to resources at the server level – for example, in the
.htaccess file on an Apache server, used by nearly half of all active websites – to set their caching characteristics. Caching is enabled by identifying a resource or type of resource, such as images or CSS files, and then specifying headers for the resource(s) with the desired caching options.
Setting HTTP response
cache-control header in Spring framework
Only set HTTP Cache-Control header for HTTP directly response by Nginx, not proxy_pass server HTTP responses. In other words, request static files via the server’s file path. For example:
- expires 30d;
- add_header Cache-Control “public, no-transform”;
- Cache types:
ico/jpg/jpeg/png/gif: max-age=2592000 seconds (30 days) or max-age=31536000 (365 days)
css/js: max-age=86400 seconds (1 hours) or max-age=2592000 seconds (30 days)