Use nginx upstream group with multiple ports

You must define port in every server entry in upstream. If you don't nginx will set it to 80. So server 10.240.0.26; actually means server 10.240.0.26:80;.

You could define several upstream blocks though:

upstream production_1234 {
    server 10.240.0.26:1234;
    server 10.240.0.27:1234;
}
upstream production_4321 {
    server 10.240.0.26:4321;
    server 10.240.0.27:4321;
}
server {
    listen 80;
    server_name some.host;
    location / {
        proxy_pass http://production_1234;
    }
}
server {
    listen 80;
    server_name other.host;
    location / {
        proxy_pass http://production_4321;
    }
}

Another option is to configure your local DNS to resolve hostname production to several IPs, and in this case nginx will use them all.

http://nginx.org/r/proxy_pass: If a domain name resolves to several addresses, all of them will be used in a round-robin fashion.

server {
    listen 80;
    server_name some.host;
    location / {
        proxy_pass http://production:1234;
    }
}

When you use upstreams, the ports are defined in the upstream blocks:

upstream production {
    server 10.240.0.26:8080;
    server 10.240.0.27:8081;
}

In other words, nginx resolves proxy_pass argument either to a upstream group or a host:port pair. When you use a variable as argument, it only resolves to host:port pair.

Tags:

Nginx