In continuation of the answer for the "Nginx Reverse Proxy for Tumblr" question, what is the best way to cache a web-site like Tumblr with nginx, which returns distinct results depending on the User-Agent?

From the one hand, we wouldn't want to keep a distinct cache copy for every possible user-agent string -- that would be insane.

From the other, the information of what parts of the request they use to determine which version to serve, is proprietary, so it's not like we even know what we're dealing with.

What the best way to do the caching with the minimum chance of distinct user-agents polluting and disturbing the cache?

  • Some clarifications: I'm specifically looking for an nginx-only solution. It probably has to have two cache zones. Might have to do some kind of internal redirects and/or variables etc. I might look into this question more myself when I have some extra time or if anyone offers a bounty. ;-)
    – cnst
    Commented Jan 4, 2015 at 20:49
  • If you find this answer helpful to you, please accept this answer as correct.
    – Steve
    Commented Mar 25, 2017 at 22:27
  • @Steve, thanks, but the existing answer is not at all helpful, unfortunately; i plan to produce an actual answer myself, but haven't gotten to it yet; do you know why was this bumped (and by whom)?
    – cnst
    Commented Mar 25, 2017 at 22:34
  • the "community" bot bumps questions with no answer to the front page. My comment is part of an attempt to see more answers be accepted, it makes the site more useful to visitors.
    – Steve
    Commented Mar 26, 2017 at 5:46

1 Answer 1


You are correct that proxying less effective when the document has lots of variations for various clients. In this case it sounds like there might only be two versions, which makes the problem a little easier. The ideal solution would proxy based on user agent. It would serve the mobile version to mobile clients and the desktop version to other clients.

For example there are regular expressions that could be used in nginx rewrite rules to do the proxying. Such rules are available here.

Here is how you might implement it in nginx:

if ($http_user_agent ~* "/Mobile|Android|BlackBerry/") {
    proxy_pass    http://m.example.com$request_uri;

proxy_pass    http://www.example.com$request_uri;
  • I'm looking for a more concrete answer than a link to some obscure external page on regular expressions.
    – cnst
    Commented Jan 4, 2015 at 20:39
  • I've added more detail to the answer. I'm not sure what you are using in nginx for caching proxy requests, I don't think that the proxy_pass directives I included would do that. Commented Jan 4, 2015 at 22:10
  • The answer is now simply invalid -- tumblr doesn't have m. -- they have the same URLs for both mobile and non-mobile, so, I'm not sure how your detail is supposed to help at all here. And, yes, proxy_pass by itself don't cache anything, either, so, this is just plain wrong!
    – cnst
    Commented Jan 5, 2015 at 5:39
  • Can you provide more info in your question, such as which module and configuration you are using for caching proxy? It should just be a matter of plugging that in place of proxy_pass. Commented Jan 5, 2015 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.