I just read this Digital Ocean article and was wondering if the topology in the final example can be implemented with nginx.

I'm interested in how the load balancer handles the two dedicated cache servers that are NOT in front of the application servers. They describe the process like this:

  1. The load balancer checks cache-backend to see if the requested content is cached (cache-hit) or not (cache-miss)
  2. If cache-hit: return the requested content to the load balancer and jump to Step 7. If cache-miss: the cache server forwards the request to app-backend, through the load balancer
  3. The load balancer forwards the request through to app-backend
  4. app-backend reads from the database then returns requested content to the load balancer
  5. The load balancer forwards the response to cache-backend
  6. cache-backend caches the content then returns it to the load balancer
  7. The load balancer returns requested data to the user

I guess the load balancer should have the two groups in upstream directives:

upstream cachebackend {
    server cache-1.example.com;
    server cache-2.example.com;

upstream appbackend {
    server app-1.example.com;
    server app-2.example.com;

and then from inside a server directive:

location / {
    proxy_pass http://cachebackend;

    # if that one is a MISS, request this one:
    # proxy_pass http://appbackend;
    # and then save the response on the cachebackend
    # before returning it to the client

I want to know how to tell nginx to follow the steps above, or if it's possible at all.

Thanks :)

1 Answer 1


Nginx can load-balance and cache simultaneously, you just need to configure cache zone using proxy_cache_path directive and assign it to the particular server {} or location {} using proxy_cache. So, concluding, if using nginx as both load balancer and cache, Digital Ocean architecture looks way redundant.

  • So it's not possible? Or it's just that it doesn't make sense? Sep 24, 2015 at 10:26
  • It is possible, but it makes no sense, because "pure proxy" would be nginx that just proxies, and "cache server" would be an nginx that proxies (because it needs to take objects if it doesn't have them in it's cache) and caches. Thus, first category isn't needed unless you want to split load for some specific reason, however, I cannot imagine one.
    – drookie
    Sep 24, 2015 at 10:38
  • So for nginx to act as a cache server it has to be in front of application servers and not "next to" application servers. Makes sense. I'm not sure about best practices on putting load balancing and caching on the same machine but that's another question. Thanks :) BTW can't upvote you, not enough rep yet! Sep 25, 2015 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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