Server Fault is a question and answer site for system and network administrators. 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 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
share|improve this question
"obviously it's not the correct solution" But why? – VBart Nov 7 '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 7 '12 at 18:51
Because /ID ($request_uri) isn't the same key as ID (~^/prefix/(?<id>.+)$). – VBart Nov 7 '12 at 19:19
up vote 0 down vote accepted
map $request_uri $page_id {
    default $request_uri;
    ~^/prefix(?<id>/.+)$ $id;

Note that leading /.

share|improve this answer
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 7 '12 at 19:44
Well that was easy. Thanks! – Josh French Nov 7 '12 at 22:45

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.