Nginx – Explain load-balancing with Nginx like I’m five

nginxruby-on-railsunicorn

I've found plenty of DIY posts and tutorials on how to configure Nginx as a load-balancer using upstream server:

upstream backend  {
  ip_hash;
  server 1.2.3.4;
  server 1.2.3.5;
  server 1.2.3.6;
}

server {
  location / {
    proxy_pass  http://backend;
  }
}

But that's the extent to what I can find as far as configuring this architecture. Currently I have a rails application deployed to 3 backend VPS servers and I'm using Unicorn for my HTTP server. Do I need my ruby installed along with my rails application and unicorn on my load-balancing server as well? Do I need Nginx to be installed on each of the upstream servers? If so, how do I configure them? If I introduce something like Varnish to the architecture, where does that go? In front of the load-balancer or each of the backends?

Here's a visual of how I have everything organized:

                                       +---> backend1 <---+
                                       |                  |
[requests] <---> [Nginx load-balancer]-+---> backend2 <---+-[Database server]
                                       |                  |
                                       +---> backend3 <---+

Best Answer

Short answer:

Nginx really only has one job, and that job is to accept an incoming request and hand it off to a backend server.

Given that, your front end server(s) only need to run nginx and your backend server(s) only need to run rails. Make sense?

Now, if your backend and frontend are both running on the same server then, yes, of course, it would have to be installed there too, but from your diagram I do not believe that is the case.

If you introduce an HTTP caching software, such as Varnish, it would go between Nginx and Rails, most likely also running on the backend server. So requests would follow this path:

Nqinx -> Varnish -> Rails
Related Topic