>_ IzzyLab

Hands-on task

This task is step 16 of the course “nginx basics”

Serve gzip and cache static files

medium~20 min

Optimize how the site is served: enable gzip in nginx for text types and add the Cache-Control caching header with a long lifetime (at least 7 days) to static files (js/css). nginx is on port 80.

What you'll practise:
  • nginx
  • gzip
  • cache-control
  • expires
  • serving optimization

Topic breakdown

Serving static files is optimized at the nginx config level via two independent mechanisms. Compression (gzip on;) reduces the size of text responses on the fly, but by default only affects HTML — the gzip_types directive with a list of MIME types lets you extend the set of compressed formats. Caching sets the browser's Cache-Control: max-age header, allowing it to keep rarely-changing files and not bother the server; in nginx it's convenient to set with the expires directive inside a location that catches the right extensions. After the edits the config is checked (nginx -t) and reloaded.

What's checked

  1. style.css and app.js are served with gzip
  2. .css and .js get Cache-Control with a max-age of at least 7 days

What it looks like