websites/ewintr.nl/content/2020/basic-caching-headers-in-ng...

1.6 KiB

+++ title = "Basic caching headers in nginx" date = 2020-01-05 +++

To add basic caching headers for different filetypes, add an expires directive to your nginx config file, like this:

{% code() %}

Expires map

map $sent_http_content_type $expires { default off; text/html epoch; text/css 30d; application/javascript 30d; ~image/ 30d; ~font max; }

server { listen 80 default_server; listen [::]:80 default_server;

expires $expires;
...

{% end %}

  • off means no caching headers.
  • epoch is no caching, ask the website itself.
  • 30d cache for 30 days.
  • max is the maximum, cache as long as you can.
  • A ~ in the mimetype indicates a regular expression.

Fonts

It could be that this does not work right away for fonts, as nginx defaults to the application/octet-stream mimetype for those filetypes. To fix this, add these lines to the /etc/nginx/mime.types config file:

{% code() %} font/ttf ttf; font/opentype otf; font/woff woff; font/woff2 woff2; {% end %}

Don't forget to add the first two to the list of gzipped mimetypes, the last two already have compression baked into the format:

{% code() %} gzip_types text/plain text/css ... font/ttf font/opentype; {% end %}

In /etc/nginx/nginx.conf (on Debian).

Sources