How to use Nginx proxy server in Docker to achieve load balancing of multiple web servers?

WBOY
Release: 2023-09-05 12:46:01
Original
828 people have browsed it

How to use Nginx proxy server in Docker to achieve load balancing of multiple web servers?

How to use Nginx proxy server in Docker to achieve load balancing of multiple web servers?

Abstract:
In the architecture of modern web applications, load balancing is an important topic. By distributing traffic across multiple servers, you can improve system availability, performance, and scalability. This article will introduce how to use Docker and Nginx proxy server to achieve load balancing of multiple web servers.

  1. Preparation
    First, we need to install Docker and Nginx. You can find the corresponding installation guide on the official website.
  2. Create multiple Web servers
    Before we implement load balancing, we need to create multiple Web servers first. Docker can be used to create multiple web server instances. The following is a sample Dockerfile for creating a simple web server image.
FROM nginx:latest
COPY index.html /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
Copy after login

Here, we use the official image provided by Nginx and copy the files index.html and nginx.conf to the corresponding locations. In index.html, you can place any web content you want to display. In nginx.conf, you can configure Nginx related settings.

  1. Configure Nginx proxy server
    Next, we need to configure Nginx proxy server to achieve load balancing. Create a file called nginx.conf and add the following content to the file.
http {
  upstream backend {
    server web1:80;
    server web2:80;
  }
  server {
    listen 80;
    location / {
      proxy_pass http://backend;
      proxy_set_header Host $host;
    }
  }
}
Copy after login

Here, we define an upstream server group named backend, which contains the addresses of the two web servers. In the server block, we bind port 80 to the Nginx proxy server and proxy traffic to the backend server group. The proxy_set_header directive is used to set the Host header of the request to the address of the server.

  1. Running multiple web servers and Nginx proxy servers
    After completing the configuration, we can use Docker to run multiple web server instances and one Nginx proxy server instance.

First, we need to build the web server image. In the directory where the image is located, run the following command.

docker build -t web-server .
Copy after login

Then, we can run multiple web server instances. Run the following command twice to create two instances.

docker run -d --name web1 web-server
docker run -d --name web2 web-server
Copy after login

Next, we need to create an Nginx proxy server instance. Run the following command.

docker run -d -p 80:80 --name nginx-proxy --link web1 --link web2 nginx
Copy after login

Here, we used the --link parameter to connect the Nginx proxy server to the two web server instances.

  1. Test Load Balancing
    Now, we can use any web browser to test load balancing. Simply enter the address of the Nginx proxy server and traffic will be distributed to both web server instances.

By looking at the logs of the web server container, we can see how requests are distributed to different instances.

docker logs web1
docker logs web2
Copy after login

Conclusion:
By using Docker and Nginx proxy server, we can easily achieve load balancing of multiple web servers. This method is not only simple and efficient, but also highly flexible and scalable. I hope this article can be helpful to you, thank you for reading!

The above is the detailed content of How to use Nginx proxy server in Docker to achieve load balancing of multiple web servers?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!