Expiration headers when rewriting requests

Hello,

I’m having trouble translating my setup into a working NginX config.
Any help is much appreciated – I’m almost there, but not quite.

I need to rewrite URLs like /r1234567/xyz… to be simply /xyz… to
allow for versioned caching of resources (cache forever, change
revision in URL to force cache update).

A simple rewrite rule works to find the resource, but I need to set
the expiration headers to max for requests like this. I tried doing
this:

location ~ ^/r[0-9]+/ {
expires max;
add_header Last-Modified “Thu, 01 Jan 1970 00:00:01 GMT”
rewrite ^/r[0-9]+(/.*)$ $1 last;
}

location / {
# check for a cached file, fallback to FastCGI, etc
}

But this doesn’t work because the rewrite seems to wipe out the
headers when starting the request over. I tried the rewrite with
last, break and no flags – nothing worked.

Any ideas?

Thanks in advance,
Brian

On Tue, Mar 11, 2008 at 04:27:46PM -0500, Brian Kirkbride wrote:

A simple rewrite rule works to find the resource, but I need to set
}

But this doesn’t work because the rewrite seems to wipe out the
headers when starting the request over. I tried the rewrite with
last, break and no flags – nothing worked.

After rewrite nginx uses new location configuration unless you use
break:

location ~ ^/r[0-9]+/ {
expires max;
add_header Last-Modified “Thu, 01 Jan 1970 00:00:01 GMT”

  • rewrite ^/r[0-9]+(/.*)$ $1 last;
  • rewrite ^/r[0-9]+(/.*)$ $1 break;
    }

However, it will not work after fallback to FastCGI.

Igor S. wrote:

Thanks you Igor, that is what I thought. FastCGI is not a problem as
I can have the Application set the Expires headers correctly in that
case.

My problem is that I have things in the “location /” block that need
to act on /r1234123/… requests after they have been rewritten. This
is not possible if I want to retain the expires setting in the
“location ~ /^r[0-9]+/” block.

I will duplicate the configuration in “location /” to the other
location block. That should work, thanks!

  • Brian