1

I have a site with some URLs that look like this: /prefix/ID, where /prefix is static and ID is unique. Using Nginx as a reverse proxy, I'd like to cache these pages at the /ID portion only, omitting the prefix.

Can I configure Nginx so that a request for the original URL is cached at the shortened URL?

I tried this (I'm omitting some irrelevant parts) but obviously it's not the correct solution:

http {
    map $request_uri $page_id {
        default $request_uri;
        ~^/prefix/(?<id>.+)$ $id;
    }

    location / {
        proxy_cache_key $page_id
    }
}
Josh French
  • 113
  • 3
  • _"obviously it's not the correct solution"_ But why? – VBart Nov 07 '12 at 13:30
  • ...or I wouldn't need to ask :) Not sure what's happening under the hood, but when I set the cache key like this it serves cached content at `/prefix/ID` but a request for `/ID` is still passed to the app and returns a 404. – Josh French Nov 07 '12 at 18:51
  • Because `/ID` ($request_uri) isn't the same key as `ID` (~^/prefix/(?.+)$). – VBart Nov 07 '12 at 19:19

1 Answers1

0
map $request_uri $page_id {
    default $request_uri;
    ~^/prefix(?<id>/.+)$ $id;
}

Note that leading /.

VBart
  • 8,309
  • 3
  • 25
  • 26
  • 1
    it may be a bit helpful to provide a bit more instruction here. The site automatically flagged this is low quality because you didn't put much content into the post. Maybe an additional set of instructions would be helpful. – Brent Pabst Nov 07 '12 at 19:44