Operations 5 min read

Understanding Load Balancing and Its Implementation with Docker and Nginx

This article explains the concept and importance of load balancing, then demonstrates a practical Docker‑Compose setup with multiple PHP containers and an Nginx reverse proxy, including configuration files and test results that show how traffic is distributed to improve system reliability and performance.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding Load Balancing and Its Implementation with Docker and Nginx

Load balancing is a strategy that distributes work tasks or network traffic evenly across multiple computing resources or servers to achieve higher overall performance, better resource utilization, optimized response times, and prevention of overload on any single node.

As applications grow and user numbers increase rapidly, the demand for server resources can surge, potentially causing server overload or failure, especially under heavy concurrent requests. Without proper load balancing, servers may be unable to handle the load effectively.

Introducing a load balancer becomes crucial; it helps analyze and optimize system performance, distribute requests appropriately, avoid single points of failure, and enhance overall system availability and scalability.

Implementing Load Balancing

In a Docker environment, the author configures a docker-compose.yml file to achieve load balancing. The detailed Docker configuration is shown below.

version: '3'

services:
  php1:
    build:
      context: .
      dockerfile: php/Dockerfile
    image: php
    container_name: php1
    restart: unless-stopped
    volumes:
      - ./php:/var/www/app/
    environment:
      - SERVICE_ID=1
      - PORT=8000
    networks:
      - app_net

  php2:
    build:
      context: .
      dockerfile: php/Dockerfile
    image: php
    container_name: php2
    restart: unless-stopped
    volumes:
      - ./php:/var/www/app/
    environment:
      - SERVICE_ID=2
      - PORT=8001
    networks:
      - app_net

  nginx-php:
    build:
      context: .
      dockerfile: nginx/Dockerfile
    image: nginx:alpine
    container_name: nginx-php
    restart: unless-stopped
    volumes:
      - ./php:/var/www/app/
      - ./nginx/conf/:/etc/nginx/conf.d/
    networks:
      - app_net
    ports:
      - 80:80
    depends_on:
      - php1

networks:
   app_net:
    driver: bridge

This configuration uses two PHP containers to thoroughly test Nginx's load‑balancing capability.

php.Dockerfile

FROM php:8.1-cli

COPY . /var/www/app/start.sh

WORKDIR /var/www/app

RUN chmod +x ./start.shchmod +x ./start.sh

CMD ["sh", "./start.sh"]

start.sh

#!/bin/bash

set -e;

php -S 0.0.0.0:$PORT ./routes.php;

The most critical part of the setup is the Nginx configuration.

Nginx.Dockerfile

FROM nginx:1.23.1-alpine

RUN rm /etc/nginx/conf.d/default.confrm /etc/nginx/conf.d/default.conf

default.conf

# docker-basic-lb/proxy/default.conf.conf

upstream gateway {
  server php1:8000;
  server php2:8001;
}

server {
  location / {
    proxy_pass http://gateway;
  }
}

The resulting directory structure is:

C:.
├───nginx
│   └───conf
└───php

Test Results

First Hit / Product Endpoint

(Image showing first request distribution)

Second Click / Product Endpoint

(Image showing second request distribution)

From the test results, when the first server becomes busy, requests are intelligently redirected to the less‑loaded server, ensuring that traffic does not pile up on a single machine. This improves overall response capability, stability, user experience, and reduces the risk of single‑point failures, providing more reliable service for the product.

backendDockerOperationsLoad BalancingNginx
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.