Nginx: Can I cache a URL matching a pattern at a different URL?

Posted by Josh French on Server Fault See other posts from Server Fault or by Josh French
Published on 2012-11-06T03:43:04Z Indexed on 2012/11/06 5:05 UTC
Read the original article Hit count: 566

Filed under:
|
|

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
    }
}

© Server Fault or respective owner

Related posts about nginx

Related posts about rewrite