Home > OS >  nginx load balancer configuration not working
nginx load balancer configuration not working

Time:09-23

I have a very simple load balancing configuration, set it up for PoC purpose. My app server1 and load balancer server is same.Below is my load balncer conf file content. Please help me is this correct?

At the moment, whenever all my request goes to IP1. I expect it to route traffic to IP2 as well whenever I hit IP1, please correct if this understanding is wrong.

upstream myapp1 {
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
            }

server {
    listen 80;

    location / {
        proxy_pass http://myapp1;
                }
}

CodePudding user response:

Your configuration is correct. Sending multiple requests to your NGINX Proxy Port 80 will Loadbalance the traffic with the default LB-Algorithem round-robin to one of your backend (upstream) servers.

Check this out: https://www.nginx.com/resources/wiki/start/topics/examples/loadbalanceexample/

http {
  upstream myproject {
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;
  }

  server {
    listen 80;
    server_name www.domain.com;
    location / {
      proxy_pass http://myproject;
    }
  }
}

You can try this from any Linux command line

for ((i=1;i<=10;i )); do curl -v "http://localhost"; sleep 1; done

This should print AppServer1, AppServer2, AppServer3 and start again from 1.

A demo-backend could look like

server {
    listen 8080;
    location / {
      return 200 "AppServer1\n";
    }
  }

server {
    listen 8081;
    location / {
      return 200 "AppServer2\n";
    }
  }

server {
    listen 8082;
    location / {
      return 200 "AppServer3\n";
    }
  }

I have just tested in a fresh nginx docker container without any problem.

  • Related