nginxportload-balancing

how to load balance 3 ports on localhost in nginx?


I have 3 ports loading different content and I want to load balance them from port 8080. I am new to nginx and I feel like there is something fundamental I'm not understanding here when it comes to serving content on nginx. My 3 ports work separately but not the load balancing. How can I edit my configuration to get load balancing between these 3 ports?

upstream backend {
    server localhost:8081;
    server localhost:8082;
    server localhost:8083;
}

server {
    listen 8080;
    listen [::]:8080;

    server_name _;

    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

# Default server configuration
#
server {
    listen 8081 default_server;
    listen [::]:8081 default_server;

    root /var/www/n81;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

}

server {
    listen 8082 default_server;
    listen [::]:8082 default_server;

    root /var/www/n82;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

}

server {
    listen 8083 default_server;
    listen [::]:8083 default_server;

    root /var/www/n83;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

}


Solution

  • Take a look at Nginx official docs here. I think you misunderstood the concept of load balancing.

    I assume you have 4 VPS, and Nginx installed on each of them. One of them for load balancing and 3 of them for serving files.

    Your final Nginx configuration files could look like this:

    Load Balancer

    http {
        upstream myapp {
            server srv1.example.com;
            server srv2.example.com;
            server srv3.example.com;
        }
    
        server {
            listen 80;
    
            location / {
                proxy_pass http://myapp;
            }
        }
    }
    

    Content Server 1

    http {
        server {
            server_name srv1.example.com;
            listen 80;
    
            location / {
                try_files $uri =404;
            }
        }
    }
    

    Content Server 2

    http {
        server {
            server_name srv2.example.com;
            listen 80;
    
            location / {
                try_files $uri =404;
            }
        }
    }
    

    Content Server 3

    http {
        server {
            server_name srv3.example.com;
            listen 80;
    
            location / {
                try_files $uri =404;
            }
        }
    }