Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Fix the "Specify a Cache Validator" Warning

Speed tools want your static files to carry a Last-Modified or ETag header. Add one in Apache or Nginx.

Speed and Performance2 min read2 steps4 screenshots

What cache validators are

A cache validator lets a browser ask the server "has this file changed?" instead of downloading it again. There are two kinds:

  1. Last-Modified. The date of the last change of the file.
  2. ETag. A unique tag for the version of the file. It is more exact.

If a file has neither, the browser must download it again every time. Tools such as PageSpeed Insights then show the warning.

Find the affected files

Run your site through PageSpeed Insights or GTmetrix. The report lists the resources without a validator.

PageSpeed Insights listing resources without cache validators

Most web servers send these headers by default for static files. If yours does not, add them.

Apache

Enable the mod_headers and mod_expires modules. Add this to your .htaccess or your site config:

Editing the .htaccess file
# Keep the default ETag and Last-Modified for static files
<IfModule mod_headers.c>
  FileETag MTime Size
  <FilesMatch "\.(html|css|js|jpg|jpeg|png|gif)$">
    Header set Cache-Control "public, max-age=31536000"
  </FilesMatch>
</IfModule>
The Apache rules

FileETag MTime Size makes Apache add an ETag from the modified time and the size. Apache sends Last-Modified for static files on its own.

Nginx

Nginx sends Last-Modified and ETag for static files by default. Make sure they are on, and add cache headers:

location ~* \.(html|css|js|jpg|jpeg|png|gif)$ {
  etag on;
  if_modified_since exact;
  expires 1y;
  add_header Cache-Control "public";
}
The Nginx rules

Then test the config and reload:

sudo nginx -t
sudo systemctl reload nginx

Improve caching

  • Cache-Control. Set a long lifetime for static files that do not change:
Cache-Control: max-age=31536000, public
  • CDN. A CDN adds the right headers and serves files from many places.
  • Cache policy. Use short times for pages that change and long times for versioned assets.

Check the result

Test a file:

curl -I https://example.com/style.css

The answer should show Last-Modified or ETag.

Note: Do not set a "Last-Modified" value to the current time on every request. That defeats the cache.

Quick recap

  • Static files need Last-Modified or ETag.
  • Apache and Nginx can add both.
  • Check with curl -I.