1、配置nginx通过不同path反代至不同后端tomcat(即访问www.jiangshen.com/a/反代至tomcat1,访问www.jiangshen.com/b/反代至tomcat2

1.1 环境准备
nginx反代服务器    10.0.0.177
tomcat-1          10.0.0.175
tomcat-2          10.0.0.176
1.2 安装nginx反代服务器
[root@nginx-1 ~]#yum install nginx -y
[root@nginx-1 ~]#systemctl enable --now nginx

image.png

1.3 安装两台tomcat服务器
#tomcat-1准备下面三个文件
[root@nginx-2 ~]#ll
total 200616
-rw-r--r--  1 root root  10379806 Aug 17 18:43 apache-tomcat-8.5.57.tar.gz
-rw-r--r--  1 root root      2250 Aug 17 18:43 install_tomcat_for_centos.sh
-rw-r--r--  1 root root 195013152 Aug 17 18:43 jdk-8u212-linux-x64.tar.gz

[root@nginx-2 ~]#cat install_tomcat_for_centos.sh 
#!/bin/bash
#
#********************************************************************
#Author:		jiangshen
#QQ: 			1461918614
#Date: 			2021-08-17
#FileName:		install_tomcat_for_centos.sh
#Description:		The test script
#Copyright (C): 	2021 All rights reserved
#********************************************************************
. /etc/init.d/functions
DIR=`pwd`
JDK_FILE="jdk-8u212-linux-x64.tar.gz"
TOMCAT_FILE="apache-tomcat-8.5.57.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"
install_jdk(){
if ! [ -f "$DIR/$JDK_FILE" ];then
	action "$JDK_FILE 文件不存在" false
	exit;
else
	[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk1.8.* jdk

cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\$JAVA_HOME/jre
export CLASSPATH=\$JAVA_HOME/lib/:\$JRE_HOME/lib/
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
. /etc/profile.d/jdk.sh
java -version && action "JDK 安装完成" || { action "JDK 安装失败" false ; exit; }
}

install_tomcat(){
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
	action "$TOMCAT_FILE 文件不存在" false
	exit;
elif [ -d $TOMCAT_DIR/tomcat ];then
	action "TOMCAT 已经安装" false
	exit
else
	[ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd $TOMCAT_DIR && ln -s apache-tomcat-*/ tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH' > /etc/profile.d/tomcat.sh
id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat

cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JDK_DIR/jdk
EOF

chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/

cat > /lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target

[Service]
Type=forking
EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now tomcat.service
systemctl is-active tomcat.service &> /dev/null && action "TOMCAT 安装完成" || { 
action "TOMCAT 安装失败" false ; exit; }

}
install_jdk
install_tomcat

[root@nginx-2 ~]#bash install_tomcat_for_centos.sh

#tomcat-2准备和上面一样的三个文件

#测试

image.png image.png

1.4 nginx用http反代至tomcat
#在nginx里面配置
[root@nginx-1 ~]#cat /etc/nginx/conf.d/proxy.conf 
server {
	listen 80;
	server_name www.jiangshen.com;
	location /a {
	index index.html;
	proxy_pass http://10.0.0.175:8080/;    #注意有后面的/, 表示置换
	}				       #后面没有 / , 表示附加
	location /b {
	index index.html;
	proxy_pass http://10.0.0.176:8080/;
	}
}

#在tomcat-1
[root@nginx-2 ~]#cat /usr/local/tomcat/webapps/ROOT/index.html
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>tomcat</title>
</head>
tomcat1

#在tomcat-2
[root@nginx-3 ~]#cat /usr/local/tomcat/webapps/ROOT/index.html
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>tomcat</title>
</head>
tomcat2

image.png image.png