Score:0

Load balance site with Nginx

cn flag

I have a rails 4 application served by nginx that is getting more traffic than the server can handle so i want to run a second instance of that app on another server and load balance the traffic between the two.

This rails app serves a lot of sites coming from a lot of different domains, each site has its own Nginx server block with SSL certificates that proxy_pass the traffic back to the rails app so for example mysite.com goes to mainapp.com/1, myothersite.com goes to mainapp.com/2 and so on.

my idea would be to modify the nginx configurations for the rails app on server #1 to act as a load balancer, by adding an upstream block with the IPs of the two servers that run the rails app (one of the IPs would be 127.0.0.1 since its the same server) so the traffic would either go to itself or to server #2

Now what i fail to understand is what to put in the upstream block because if i do something like this:

upstream samplecluster {
  ip_hash;
  server localhost;
  server x.xx.xxx.x; #private IP of server #2
}

considering server #2 is running more sites, how would Nginx know which site to serve when receiving the traffic from the load balancer?

would the solution be to use domains instead of IP for server #2? Like for example:

upstream samplecluster {
  ip_hash;
  server localhost;
  server www2.mainapp.com; #domain instance of the main app on server #2
}

and on the server block for the mainapp instance on server #2 use www2.mainapp.com as the server_name?

Or maybe use the internal IP but to a specific port and then use server_name _; and listen only to that specific port?

Something like this maybe?


#on server #1

upstream samplecluster {
  ip_hash;
  server localhost:6789;
  server x.x.x.x:6789; #internal ip of server #2
}

#main app domain acting as load balancer
server {
  listen 443;
  server_name www.mainapp.com mainapp.com;

  location / {
    try_files $uri @app;
  }

  location @app {
    proxy_pass http://samplecluster;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Host $http_host;

    proxy_headers_hash_max_size 512;
    proxy_headers_hash_bucket_size 128;

    proxy_redirect off;
  }
  
  (...)
  
}

#also on server #1, main app behind load balancer actually serving the files
server {
  listen 6789;
  server_name _;
  root /var/www/mainapp;
  
  (...)
  
}


#on server #2

server {
  listen 6789;
  server_name _;
  root /var/www/mainapp;
  
  (...)
  
}

Would this work or am i missing something?

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.