1

I could not find a solution for the issue so this is my question.

Say I have 3 servers, 2 of them are upstream servers, and 1 is a load balancer.

This is my basic configurations

Upstream servers

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Load Balancer

worker_processes  auto;

worker_rlimit_nofile 100000;

error_log off;

events {
    worker_connections  5000;
    multi_accept on;
    use epoll;
}


http {
    upstream myapp1 {
        server 138.197.122.195;
        server 138.197.122.293;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
        }
    }
}

What I want to achieve is to send/redirect users' requests to the upstream servers using round robin load balancing feature of nginx. The upstreams will then serve content directly to users.

The role of load balancer here is not to reduce the load for upstreams by proxying, but to equally distribute requests to them. However, with these configurations all the requests & responses will go through the load balancer, consume its bandwidth. Is there another way to round robin redirect requests with NGINX? Please help.

The reason I want to do this is I have a limited bandwidth for a decent load balancer (high cost) thus cannot effectively scale. For example say 3 servers have 100mbps bandwidth each, if one user consumes 1mbps, the load balancer will be a bottleneck if there are 101 users. If I can distribute the traffic equally to both upstreams, the system can serve up to 200 users theoretically.

In most similar topic I found ppl using round robin DNS method. I tested it but it is not reliable because of record caching in the DNS hierarchy itself, as well as client-side address caching and reuse.

5
  • 1
    It is completely normal that all the requests and responses go through your loadbalancer. That is the main point. All the guests will always arrive to your loadbalancer. You should always keep in mind, the speed how the loadbalancer connects to the World and the speed between the loadbalancer and the actual servers will determine your connection speed. The slowest among these will determine how fast your sites will load. Also, I would exclude the /etc/nginx/conf.d/*.conf line in your loadbalancer. It is simply not needed. Can you reach both servers from the loadbalancer? (ping, ssh) – Bert Nov 20 '17 at 12:32
  • Thanks for the advice, I'll remove that line. Yes I acknowledge the role of a load balancer. However I'm not going to use a balancer for its main role, just want the round robin distribution feature. Is NGINX able to redirect requests to multiple upstream servers and let the servers handle the rest? I mean a load balancer is not necessary in my situation, whichever way to round robin redirect requests will help. – David Tran Nov 20 '17 at 13:27
  • @DavidTran Am I understanding that you want a client to hit example.com and be redirected (at a browser level) to server1.example.com or server2.example.com? Maybe something like this, then? nginx.org/en/docs/http/ngx_http_split_clients_module.html – ceejayoz Nov 20 '17 at 13:33
  • Yes exactly, and users will be redirected based on round robin algorithm. But I'm not sure how the module would work in this situation. Could you please explain further? – David Tran Nov 20 '17 at 14:31
  • nginx.com/blog/… check method 2 – Alexey Ten Nov 20 '17 at 16:52
0

You are mistaken a little bit. Round Robin is a loadbalancing method. What you wish to achieve is something called DNS Round Robin.

But even here, you can see on the link, you'll have your traffic going through your loadbalancer because that is the whole point. You can try to leave the loadbalancer out and install only the two servers but then you have to set both of their IP addresses for the exact same domain name. While this could achieve what you wanted, it won't do any good to you because it has the chance that a user session will be dropped and picked up by the other server during mid-access. And we didn't talk about database replications so far which is also a problem in this case.

My recommendation is to invest a bit more into the loadbalancer servers bandwidth and then you won't have any issues. Even further, I would use the Session persistence so your users will always stay on the server they have joined to.

Nginx as loadbalancer

1
  • Thanks. DNS Round Robin is exactly what I'm trying to achieve, but on server side as I think it would give more control, not by adding multiple records to a domain (it's not reliable as you mentioned). Seems like it is impossible with NGINX. I just read HAProxy documentation which has something like IP address load balancing, will try that out. EDIT: What I'm trying to do is similar to Elastic Load Balancer from Amazon docs.aws.amazon.com/elasticloadbalancing/latest/userguide/… – David Tran Nov 20 '17 at 14:13

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.