NGINX for the Developer
[ 166 ]Knowing what you do about the files your application generates, you can set these
headers appropriately. Let's take an example application where the main page should
be cached for 5 minutes, all JavaScript and CSS files for 24 hours, each HTML page for
3 days, and each image for as long as possible:
server {root /home/www;location / {# match the index.html page explicitly so the *.html below
# won't match the main page
location = /index.html {expires 5m;}# match any file ending in .js or .css (Javascript or CSS
files)
location ~* /.*\.(js|css)$ {expires 24h;}# match any page ending in .html
location ~* /.*\.html$ {expires 3d;}}# all of our images are under a separate location (/img)
location /img {expires max;}}