Nginx uses reverse proxy to implement load balancing process analysis

Nginx uses reverse proxy to implement load balancing process analysis

Introduction

Based on docker container and docker-compose, you need to learn the basic use of docker in linux environment

Use two tomcat as load balancing servers

1. Use docker to pull tomcat and nginx images

Pull nginx reference

Pull tomcat reference

2. Use docker-compose to create two tomcat services

Create a new tomcat directory, create a docker-compose.yml file in the directory, and enter the following content:

version: '3'
services:
  tomcat1:
    image: tomcat
    container_name: tomcat1
    ports:
      - 9090:8080

  tomcat2:
    image: tomcat
    container_name: tomcat2
    ports:
      - 9091:8080

Run the following command in the same directory as the docker-compose.yml file to start the container (-d means running in the background)

docker-compose up -d

After success, check the docker container list

Order

docker ps

The result example has two containers, tomcat1 and tomcat2

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
271dd3610d1d tomcat "catalina.sh run" 58 minutes ago Up 58 minutes 0.0.0.0:9091->8080/tcp tomcat2
fa19d20f0022 tomcat "catalina.sh run" 58 minutes ago Up 58 minutes 0.0.0.0:9090->8080/tcp tomcat1

Enter the container interactively and modify the homepage content to distinguish between the two tomcats (tomcat is used as an example below)

Order

docker exec -it fa19d20f0022 bash

Add content to the homepage

echo "9090" >> webapps/ROOT/index.jsp

3. Create nginx service

Create a new nginx directory, create a docker-compose.yml file in the directory, and enter the following content:

version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 81:80
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf

Because docker-compose automatically treats /conf/nginx.conf as a folder, you need to create a conf directory in the nginx directory before creating the container, and create an nginx.conf file in the conf directory and enter the following content:

user nginx;
worker_processes 1;

events {
 worker_connections 1024;
}

http {
 upstream myapp1 {
  server [server ip]:9090 weight=10;
  server [server ip]:9091 weight=10;
 }
 server {

  listen 80;
  server_name [server ip];
  location / {
   proxy_pass http://myapp1;
  }
 }
}

Run in the same directory as docker-compose.yml

docker-compose up -d

4. Visit [server ip]:81, refresh several times, and observe the switching between the two tomcat services.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to implement Nginx reverse proxy and load balancing (based on Linux)
  • Nginx reverse proxy and load balancing practice
  • A brief discussion on Nginx seven-layer reverse proxy and load balancing
  • Detailed explanation of Nginx load balancing and reverse proxy configuration and optimization
  • Detailed Nginx reverse proxy and load balancing deployment guide
  • Detailed explanation of Nginx HTTP load balancing and reverse proxy configuration
  • Explanation of nginx load balancing and reverse proxy

<<:  JavaScript offset implements mouse coordinate acquisition and module dragging within the window

>>:  MySQL 5.7.24 installation and configuration method graphic tutorial

Recommend

What is a MySQL index? Ask if you don't understand

Table of contents Overview From Binary Tree to B+...

Basic ideas and codes for implementing video players in browsers

Table of contents Preface Summary of audio and vi...

VMware vsphere 6.5 installation tutorial (picture and text)

vmware vsphere 6.5 is the classic version of vsph...

Vue development tree structure components (component recursion)

This article example shares the specific code of ...

Install and configure MySQL 5.7 under CentOS 7

This article tests the environment: CentOS 7 64-b...

How to mount a disk in Linux and set it to automatically mount on boot

Knowing that everyone's time is precious, I w...

New ideas for time formatting in JavaScript toLocaleString()

Table of contents 1. Conventional ideas for time ...

Summary of Linux Logical Volume Management (LVM) usage

Managing disk space is an important daily task fo...

How to modify the time in centos virtual machine

The one above shows the system time, and the one ...

Two solutions for Vue package upload server refresh 404 problem

1: nginx server solution, modify the .conf config...

Detailed explanation of Vue development website SEO optimization method

Because the data binding mechanism of Vue and oth...

Web Design Teaching or Learning Program

Section Course content Hours 1 Web Design Overvie...

Simple use of Vue vee-validate plug-in

Table of contents 1. Installation 2. Import 3. De...