Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my nginx config, I have serveral location blocks, e.g.

location ~* /a { ..}
location ~* /b { ..}
location ~* /c { ..}

And I want to apply the expires header to all of above blocks, e.g.

location ~* ^/.*\.(?:css|js|jpg|jpeg|gif|png)$ { 
    expires 1y;

Since in nginx, location blocks are exclusive to each other, so instead of repeating [1] the expires block in each of my locations above, are there better way to write the config?


location ~* /a {
    location ~* ^/.*\.(?:css|js|jpg|jpeg|gif|png)$ ...

location ~* /b {
    location ~* ^/.*\.(?:css|js|jpg|jpeg|gif|png)$ ...

location ~* /c {
    location ~* ^/.*\.(?:css|js|jpg|jpeg|gif|png)$ ...
share|improve this question
up vote 2 down vote accepted

You could set it for the server context instead of each location.

Then override, if you have any location blocks where it should not apply

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.