nginx as load balancer code example

Example 1: nginx load balancer

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

    server {
        listen 80;

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

Example 2: nginx load balancer

worker_processes 4;
thread_pool default threads=50 max_queue=100000;
 
events {
  worker_connections 1024;
}
 
http {
 
  upstream webapp-service.io {
    server companies-service:3000 weight=10 max_fails=3 fail_timeout=30s;
    server jobs-service:3000 weight=10 max_fails=3 fail_timeout=30s;
    server profiles-service:3000 weight=10 max_fails=3 fail_timeout=30s;
    server users-service:3000 weight=10 max_fails=3 fail_timeout=30s;
  }
 
  server {
      listen 80;
      listen [::]:80;
      server_name webapp-service.io;
 
      location / {
        proxy_pass http://webapp-service.io;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass $http_upgrade;
      }
  }
}

Tags:

Misc Example