Current Location: Home> Latest Articles> How to Configure Nginx for Web Load Balancing Across Multiple Docker Containers

How to Configure Nginx for Web Load Balancing Across Multiple Docker Containers

M66 2025-06-13

How to Configure Nginx Proxy Server for Web Load Balancing Across Multiple Docker Containers

Introduction:
With the widespread adoption of containerization, load balancing for web services has become increasingly important. Nginx, known for its high efficiency as both a web server and reverse proxy, is a top choice for developers to implement load balancing. In this article, we'll walk through how to configure an Nginx proxy server for load balancing across multiple Docker containers.

1. Installing Docker Environment

First, we need to install Docker on the host system. Docker is an open-source containerization platform that enables developers to build, package, and run applications efficiently. For detailed installation steps, please refer to Docker's official documentation.

2. Writing the Dockerfile

Before configuring the web service, we need to write a Dockerfile for our application. A Dockerfile is a text file used to define how to build a Docker image. In the Dockerfile, we specify the base image, required dependencies, and other configurations.

Here's an example of a basic Dockerfile:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d/default.conf
COPY html /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

In this example, we are using the official Nginx base image and copying our custom nginx.conf, default.conf, and HTML files to their respective locations inside the container. We also expose port 80 and use the CMD command to start the Nginx service.

3. Configuring Nginx Proxy Server

After setting up the Docker environment, we can proceed to configure the Nginx proxy server. First, we need to create a new Nginx configuration file (nginx.conf) to define the load balancing settings.

Here is an example of the Nginx configuration file:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    upstream backend {
        server backend1:80;
        server backend2:80;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

In this configuration, we define an upstream called "backend" that includes the addresses and ports of all the backend containers. Then, within the server block, we set up a reverse proxy for incoming requests using the proxy_pass directive and pass request headers using the proxy_set_header directive.

4. Building the Docker Image

Once the nginx.conf file is ready, place it in the same directory as the Dockerfile and build the Docker image using the following command:

docker build -t my-nginx .

5. Running Multiple Containers

Before configuring the Nginx proxy server, we need to run multiple containers as backend services. You can run two containers with the following commands:

docker run -d --name backend1 my-nginx
docker run -d --name backend2 my-nginx

Now, two containers are running Nginx services.

6. Starting the Nginx Proxy Server

Next, we need to create a new container to run the configured Nginx proxy server and link it to the backend containers. You can run the Nginx proxy server with the following command:

docker run -d -p 80:80 --link backend1 --link backend2 my-nginx

At this point, all requests on port 80 of the host machine will be received by the Nginx proxy server and distributed to the backend containers based on the load balancing algorithm.

Conclusion

By configuring the Nginx proxy server, we can easily implement web load balancing across multiple Docker containers. This not only improves the performance and stability of applications but also allows us to better utilize server resources. We hope the steps and code examples provided in this article help you configure and use Nginx for load balancing effectively.