Nginx Statics Files Caching

December 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;
        }
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.