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

I am using rewrite like this

    if (!-e $request_filename) {
            rewrite ^/(en|ga)?/?(.*)$ /index2.php?cultureKey=$1&mq=$2 last;

but after I need certain rewritten URLs to be cached for 30 seconds. Remaining fast_cgi requests should be 5 minutes.

    fastcgi_cache_valid   200 302  5m;

    expires 5m;
    if ($args ~* (dynamic\.html)){
        expires 30s;

I have tried moving those around, but either both dynamic.html and other files are still cached after 30 seconds or they both expire after 30.

How to make dynamic.html (rewritten) cached for 30s and remaining files for 5m?


share|improve this question
up vote 1 down vote accepted

$args is a variable containing the request parameters on a GET, are you sure you are not after $request_uri or $uri? Maybe using a location block is better:

location ~* dynamic.html {
    expires 30s

but it depends on the rest of your config so YMMV.

share|improve this answer
how exactly config works? if i specify expires twice, which one is used? – romaninsh Apr 8 '11 at 5:54
and i have $args after rewrite. – romaninsh Apr 8 '11 at 5:55
From what I know from Nginx configuration, the most specific applies before the broader, so I think that the 30s rule will apply to the ones that match the location and the others will be matched by the outside expires of 5m, but I can't find any documentation to back me there. About your other question, you should read this and this on nginx docs to understand. – coredump Apr 8 '11 at 6:01

I think it is 'if' what causing problems. You should read this

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.