I'm trying to load balance an API server using nginx and docker's native DNS.
I was hoping nginx will round-robin API calls to all available servers. But even when I specify docker's DNS server as the resolver nginx forward the request to only one server.
Relevant section from docker-compose.yml
proxy:
restart: always
build: ./src/nginx/.
ports:
- "8080:8080"
links:
- api:servers.api
nginx.conf
worker_processes 2;
events { worker_connections 1024; }
http {
sendfile on;
server {
listen 8080;
location / {
resolver_timeout 30s;
resolver 127.0.0.11 ipv6=off valid=10s;
set $backend http://servers.api:80;
proxy_pass $backend;
proxy_redirect off;
}
}
}
NGINX round-robin load balancer works if I manually specify each server, which I don't want to do since it can't scale automatically.
worker_processes 2;
events { worker_connections 1024; }
http{
sendfile on;
upstream api_servers{
server project_api_1:80;
server project_api_2:80;
server project_api_3:80;
}
server{
listen 8080;
location / {
proxy_pass http://api_servers;
proxy_redirect off;
}
}
}
How to configure nginx in such a way that it can detect new containers added and include them in the round-robin?