Skip to content

Instantly share code, notes, and snippets.

@HuakunShen
Last active July 18, 2021 01:06
Show Gist options
  • Save HuakunShen/3647af3c0005b31e133117b296941b21 to your computer and use it in GitHub Desktop.
Save HuakunShen/3647af3c0005b31e133117b296941b21 to your computer and use it in GitHub Desktop.
Nginx Load Balancer
version: '3.9'
services:
server1:
image: nginx
volumes:
- ./index-1.html:/usr/share/nginx/html/index.html
server2:
image: nginx
volumes:
- ./index-2.html:/usr/share/nginx/html/index.html
loadbalancer:
image: nginx
volumes:
- ./loadbalancer.config:/etc/nginx/conf.d/default.conf
ports:
- 80:80
depends_on:
- 'server1'
- 'server2'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Server 1</title>
</head>
<body>
<h1>Server 1</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Server 2</title>
</head>
<body>
<h1>Server 2</h1>
</body>
</html>
upstream backend {
server server1;
server server2;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://backend;
}
}

A simple Nginx Load Balancer implemented with 3 Nginx docker containers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment