2

I'm trying to cache .css e .js files.

At the moment in this way is not working:

location /static {
     alias /var/www/ttch/assets/;
}

location ~* ^.+\.(css|js)$ {
   access_log off;
   expires max;
}

With only this config, nginx serve correctly static file without cache:

location /static {
     alias /var/www/ttch/assets/;
}

Any hints about how can I merge this two directives? Thanks.

2
  • Do you really need regex in that case? expires header can be applied to /static location. Commented Dec 7, 2014 at 9:25
  • I don't want to apply expires max to all /static/ file, but also on js and css. Commented Dec 12, 2014 at 16:59

1 Answer 1

2

Alias or root directive helps you to identify location on the file system, but location is self-contained block you can extend easily, so this will work properly:

location /static {
   alias /var/www/ttch/assets/;
   access_log off;
   expires max;   
}

If you want to serve files with particular extensions, try this one:

location ~ ^/static/(.+\.(?:css|js))$ {
  alias /var/www/ttch/assets/;
  access_log off;
  expires max;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.