Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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

I am having difficulty setting the HTTP expires headers for Nginx outside of specific server (and then location) blocks. What I want is to something like the following:

location ~* \.(png|jpg|jpeg|gif|ico)$ {
    expires 1y;

But not have to repeat it in every single server block, because I am hosting a large number of sites. I can put it in every server block, but it's not very DRY.

If I try to put that into an HTTP block or outside of all other blocks, I get "location directive is not allowed here." It seems I have to put it into a server block, and I have a different server block for every virtual host.

Any help/clarification would be appreciated.

share|improve this question

Use include.

Make one new file, includes/gfx-expires-header.conf and put the directives there:

location ~* \.(png|jpg|jpeg|gif|ico)$ {
    expires 1y;

Then in every server block, you can just put include includes/gfx-expires-header.conf.

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.