Search code examples
ruby-on-railsnginxthin

Load Balancing - Web Applications with NGINX


I have a web application ruby on rails who is not configured to multithreading. In nginx config, I set up an upstream block to be load balanced. Like this :

upstream myapp {
  server 127.0.0.1:3075;
  server 127.0.0.1:3076;
  server 127.0.0.1:3077;
}

I set up also 3 process thin with 3 ports (3075,3076,3077).

I think when my first application '127.0.0.1:3075' is busy, all the request will be balanced automatically to my second application '127.0.0.1:3076' or the third one.

But load balancing is not work, even though my three web applications are running correctly independent.

Please help me to find errors.

------------------- nginx config --------------------

upstream myapp_hosts {
    server  127.0.0.1:3075;
    server  127.0.0.1:3076;
    server  127.0.0.1:3077;
}

server {
    listen       80;
    server_name  myapp.mydomain.com;
    rewrite ^(.*)$ https://myapp.mydomain.com$1 permanent; # rewrite for https, i have another bloc server listen 443.
    access_log /var/log/nginx/myapp.access.log;

    location / {
            proxy_pass         http://myapp_hosts/;
            proxy_connect_timeout   900;
            proxy_send_timeout      900;
            proxy_read_timeout      900;
            proxy_buffer_size   16k;
            proxy_buffers       32   16k;
            proxy_busy_buffers_size 64k;
    }
    location /public {
            root /var/www/nemo/;

    }
    location /images {
            root /var/www/nemo/assets/;

    }
    location /javascripts {
            root /var/www/nemo/assets/;

    }
    location /stylesheets {
            root /var/www/nemo/assets/;

    }

   client_max_body_size    10m;
   client_body_buffer_size 128k;
   client_header_buffer_size 64k;
}

Solution

  • What is the purpose of your rewrite?

    rewrite ^(.*)$ http://myapp.mydomain.com$1 permanent;
    

    It looks like it's going to constantly redirect anything to itself based off of those rules, resulting in a redirect loop. You may have mixed this line up with an HTTPS redirect configuration you found somewhere else, perhaps?

    Try removing that line and see if it works.