Nginx Statics Files Caching

Dec 9, 2017

Serve static files from a static directory, expire in 1 year.

Mapping:

  • http://localhost/static/test.jpg -> /myapp/static/test.jpg
  • http://localhost/static/js/app.js -> /myapp/static/js/app.js
server {
        ...

        location /static/ {
                alias /myapp/static/;

                expires 1y;
                add_header Cache-Control public;
                access_log off;
        }
}

Mapping:

  • http://localhost/logo.png -> /myapp/static/logo.png
  • http://localhost/img/test.jpg -> /myapp/static/img/test.jpg
server {
        ...

        location ~ \.(jpg|png|gif|js|css) {
                root /myapp/static/;

                expires 1y;
                add_header Cache-Control public;
                access_log off;
        }
}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.