Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to make nginx send expiration headers, I tried in two different ways:

location ~* ^.+\.(ico|css|js|gif|jpe?g|png)\?[0-9]+$ {
  expires max;
}

and

location / {
  if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)\?[0-9]+$") {
    expires max;
    break;
  }
  #....
}

none of which worked. No expiration headers are sent whatsoever. Any ideas how to debug this issue?

share|improve this question

4 Answers 4

Are using nginx as a caching proxy? If so be aware that upstream caching instructions are respected, but you can still instruct nginx not to using proxy_ignore_headers

proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
share|improve this answer
up vote 0 down vote accepted

These lines actually worked, I was just testing them wrong. These lines correctly try to cache only timestamped resources (those with ?id at the end) and I was looking at non-timestamped ones.

share|improve this answer

Test to make sure the locations block is being called by placing an additional directive in there..something like:

location ~* \.(ico|css|js|gif|jpe?g|png)$ {
    expires max;
    auth_basic "Restricted, please login";
    auth_basic_user_file htpasswd.users;
}

If you're not presented with a auth prompt when requesting matching files then you can be certain that the block is not being called due to an earlier break or what have you.

share|improve this answer

Location should on path only, without query string, so this shouldn't work.

Try the following :

location ~* \.(ico|css|js|gif|jpe?g|png)$ {
    expires max;
}

or, at least, the following should work too :

location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
    expires max;
}
share|improve this answer

Your Answer

 
discard

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.