Get the App
SLTechnology News&Howtos  ›  Servers  › 

Nginx uses reverse proxy to realize load balancing process parsing

Shulou Source: shulou.com Published: 2022-06-02 03:32:08 09月23日 Update

profile

Based on docker container and docker-compose, so you need to teach yourself the basic use of docker in Linux environment.

Use two tomcats as servers for Load Balancer

1. Use docker to pull tomcat and nginx images

Pull nginx reference

Pull tomcat reference

2. Create two tomcat services using docker-compose

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

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 docker-compose.yml to start the container (-d means background running)

docker-compose up -d

View docker container list after success

command

docker ps

The resulting example has two containers tomcat1 and tomcat2

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES271dd3610d1d tomcat "catalina.sh run" 58 minutes ago Up 58 minutes 0.0.0.0:9091->8080/tcp tomcat2fa19d20f0022 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 home page content to distinguish between two tomcats (tomcat is an example below)

command

docker exec -it fa19d20f0022 bash

Add content to homepage

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

3. Create nginx services

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

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

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

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 URL, refresh several times, observe the switch between the two tomcat services

The above is all the content of this article, I hope to help everyone's study, but also hope that everyone a lot of support.

Tags: Directory service content container two file server command input run home page reference balance success between background that is folder mode environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology macOS NVidia vpn