Nginx七层代理,四层代理 + Tomcat多实例部署

发布时间:2023年12月18日

目录

1.tomcat多实例部署

准备两台虚拟机

进入pc1 pc2同时安装jdk

进入pc1 pc2安装tomcat

PC1配置(192.168.88.50)

安装tomcat多实例

tomcat2中修改端口

启动tomcat1 tomcat2

分别在三个tomcat服务上部署jsp的动态页面

2.nginx的七层代理,实现动静分离

准备两台虚拟机

部署nginx基于协议的反向代理(七层代理)

nginx1(192.168.88.22)测试

静态

动态

nginx2(192.168.88.31)测试?

静态

动态?

3.nginx的四层代理,实现负载均衡

实验准备

修改配置文件

实验验证


配置图

1.tomcat多实例部署

准备两台虚拟机

192.168.88.50                     #pc1               
192.168.88.51                     #pc2

systemctl stop firewalld
setenforce 0                      #前期准备工作

进入pc1 pc2同时安装jdk

tar -xf jdk-8u361-linux-x64.tar.gz
mv jdk1.8.0_361/ /usr/local
cd /usr/local

vim /etc/profile.d/java.sh

export JAVA_HOME=/usr/local/jdk1.8.0_361
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

source /etc/profile.d/java.sh
java -version

进入pc1 pc2安装tomcat

tar -xf apache-tomcat-9.0.16.tar.gz
cp -a apache-tomcat-9.0.16 /usr/local/tomcat
cd /usr/local/tomcat/bin                        

vim /usr/lib/systemd/system/tomcat.service

[Unit]
Description=tomcat server
Wants=network-online.target
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target                   #将tomcat添加至系统服务

systemctl start tomcat
systemctl status tomcat
ss -natp|grep java                           #启动tomcat

PC1配置(192.168.88.50)

安装tomcat多实例

cd /usr/local
mv tomcat tomcat1
mkdir tomcat tomcat2
mv tomcat1 tomcat2 tomcat     
cd /opt
mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat2    #部署

cd /usr/local/tomcat/tomcat2/apache-tomcat-9.0.16
mv * ..
vim /usr/local/tomcat/tomcat1/bin/startup.sh
##21行添加##
export CATALINA_HOME=/usr/local/tomcat/tomcat1
export CATALINA_BASE=/usr/local/tomcat/tomcat1
export TOMCAT_HOME=/usr/local/tomcat/tomcat1

vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
##21行添加##
export CATALINA_HOME=/usr/local/tomcat/tomcat1
export CATALINA_BASE=/usr/local/tomcat/tomcat1
export TOMCAT_HOME=/usr/local/tomcat/tomcat1

vim /usr/local/tomcat/tomcat2/bin/startup.sh
##21行添加##
export CATALINA_HOME=/usr/local/tomcat/tomcat2
export CATALINA_BASE=/usr/local/tomcat/tomcat2
export TOMCAT_HOME=/usr/local/tomcat/tomcat2

vim /usr/local/tomcat/tomcat2/bin/shutdown.sh

##21行添加##
export CATALINA_HOME=/usr/local/tomcat/tomcat2
export CATALINA_BASE=/usr/local/tomcat/tomcat2
export TOMCAT_HOME=/usr/local/tomcat/tomcat2

tomcat2中修改端口

vim /usr/local/tomcat/tomcat2/conf/server.xml

#22行,修改Server prot,默认为8005 -> 修改为8006
<Server port="8006" shutdown="SHUTDOWN">
 
#69行,修改Connector port,HTTP/1.1  默认为8080 -> 修改为8081
<Connector port="8081" protocol="HTTP/1.1"
 
#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

启动tomcat1 tomcat2

systemctl stop tomcat           #如果刚刚用sysemctl启动了tomcat一定要先关闭在重启,不然会报错

/usr/local/tomcat/tomcat1/bin/startup.sh
/usr/local/tomcat/tomcat2/bin/startup.sh

分别在三个tomcat服务上部署jsp的动态页面

##192.168.88.50##
cd /usr/local/tomcat/tomcat1/webapps
mkdir test
vim test/123.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-    8"%>
<html>
<head>
<title>JSP test page</title>
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
</html>

cd /usr/local/tomcat/tomcat2/webapps
mkdir test
vim test/123.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-    8"%>
<html>
<head>
<title>JSP test page</title>
</head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
</body>
</html>

##192.168.88.51##
cd /usr/local/tomcat/webapps
mkdir test
vim test/123.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-    8"%>
<html>
<head>
<title>JSP test page</title>
</head>
<body>
<% out.println("动态页面 3,http://www.test3.com");%>
</body>
</html>

2.nginx的七层代理,实现动静分离

准备两台虚拟机

192.168.88.22                     #nginx1
192.168.88.31                     #nginx2
systemctl stop firewalld
setenforce 0                      #前期准备工作

部署nginx基于协议的反向代理(七层代理)

vim /usr/local/nginx/conf/nginx.conf


upstream tomcat_server{
     server 192.168.88.50:8080 weight=1;
     server 192.168.88.50:8081 weight=1;
     server 192.168.88.51:8080 weight=1;
}

location ~ .*\.jsp$ {
     proxy_pass http://tomcat_server;
     proxy_set_header HOST $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_    for;
}

location /static {
     root   html;
     index  index.html index.htm;
}

cd /usr/local/nginx/html
mkdir static
vim static/test.html

<html>
<body>
<h1>this is static web01</h1>
</body>
</html>

systemctl restart nginx

nginx1(192.168.88.22)测试

静态

动态

nginx2(192.168.88.31)测试?

静态

动态?

3.nginx的四层代理,实现负载均衡

实验准备

192.168.88.13       #安装有nginx

nginx -V
--with-stream       #有此模块

修改配置文件

vim /usr/local/nginx/conf/nginx.conf

stream {
   upstream nginx_server {
   server 192.168.88.22:80;
   server 192.168.88.31:80;
}

server {
    listen 9527;
    proxy_pass nginx_server;
}
}

systemctl restart nginx

实验验证

文章来源:https://blog.csdn.net/Ybaocheng/article/details/135005730
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。