Tomcat实现七层、四层代理的动静分离

七层代理实现动静分离

Tomcat 服务部署环境

Nginx+Tomcat负载均衡、动静分离

名称ip地址
Nginx 服务器192.168.44.120
Tomcat服务器1192.168.44.100:8080
Tomcat服务区2192.168.44.100:8081
Tomcat服务器3192.168.44.110:8080
部署tomcat
[root@localhost local]# cd /opt/
将安装 Tomcat 所需软件包传到/opt目录下
[root@localhost opt]# ls
apache-tomcat-9.0.16.tar.gzjdk-8u201-linux-x64.rpm[root@localhost opt]# systemctl stop firewalld
[root@localhost opt]# systemctl disable firewalld
[root@localhost opt]# setenforce 0
安装JDK
cd /opt
[root@localhost opt]# rpm -qpl jdk-8u201-linux-x64.rpm
[root@localhost opt]# rpm -ivh jdk-8u201-linux-x64.rpm 
[root@localhost opt]# java -version
设置JDK环境变量
[root@localhost opt]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
[root@localhost opt]# source /etc/profile.d/java.sh
[root@localhost opt]# java -version 

Tomcat多实例部署

Tomcat1

[root@localhost opt]# cd /opt/
[root@localhost opt]# tar zxvf apache-tomcat-8.5.16.tar.gz 
[root@localhost opt]# cp -a apache-tomcat-8.5.16 /usr/local/tomcat1
[root@localhost opt]# mv apache-tomcat-8.5.16 /usr/local/tomcat2/
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
bingamesliblibexecsharetomcat1
etcincludelib64sbin srctomcat2
#配置 tomcat 环境变量
[root@localhost local]# cd /etc/profile.d/
[root@localhost profile.d]# ls
256term.csh colorgrep.shlang.csh vim.csh
256term.shcolorls.csh lang.shvim.sh
abrt-console-notification.shcolorls.shless.csh vte.sh
bash_completion.shflatpak.shless.shwhich2.csh
colorgrep.csh java.sh PackageKit.shwhich2.sh
[root@localhost profile.d]# vim tomcat.sh
#tomcat1
export CATALINA_HOME1=/usr/local/tomcat1
export CATALINA_BASE1=/usr/local/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat1 

Tomcat2

 #tomcat2
export CATALINA_HOME2=/usr/local/tomcat2
export CATALINA_BASE2=/usr/local/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat2
[root@localhost profile.d]# source /etc/profile.d/tomcat.sh
修改配置文件
[root@localhost profile.d]# cd /usr/local/
[root@localhost local]# vim tomcat2/conf/server.xml 
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
<Server port="8006" shutdown="SHUTDOWN">		#22行,修改Server prot,默认为8005 -> 修改为8006
<Connector port="8081" protocol="HTTP/1.1"		#69行,修改Connector port,HTTP/1.1默认为8080 -> 修改为8081
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />	#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
启动脚本
[root@localhost local]# vim tomcat1/bin/startup.sh

Start Script for the CATALINA Server

-----------------------------------------------------------------------------

##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1

[root@localhost local]# vim /usr/local/tomcat1/bin/shutdown.sh

Stop script for the CATALINA Server

-----------------------------------------------------------------------------

##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1

[root@localhost local]# vim /usr/local/tomcat2/bin/startup.sh
-----------------------------------------------------------------------------

Start Script for the CATALINA Server

-----------------------------------------------------------------------------

##添加以下内容
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2

[root@localhost local]# vim /usr/local/tomcat2/bin/shutdown.sh

Stop script for the CATALINA Server

-----------------------------------------------------------------------------

##添加以下内容
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2

Better OS/400 detection: see Bugzilla 31132

[root@localhost local]# /usr/local/tomcat1/bin/startup.sh 
Using CATALINA_BASE: /usr/local/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME:/usr/java/jdk1.8.0_201-amd64/jre
Using CLASSPATH: /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.

[root@localhost local]# /usr/local/tomcat2/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME:/usr/java/jdk1.8.0_201-amd64/jre
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.

[root@localhost local]# netstat -natp |grep java
tcp6 00 :::8080 :::*LISTEN8268/java 
tcp6 00 :::8081 :::*LISTEN8332/java 
tcp6 00 127.0.0.1:8005:::*LISTEN8268/java 
tcp6 00 127.0.0.1:8006:::*LISTEN8332/java 
tcp6 00 :::8009 :::*LISTEN8268/java 
tcp6 00 :::8010 :::*LISTEN8332/java 

打开110配置Tomcat

[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
apache-tomcat-9.0.16.tar.gzjdk-8u91-linux-x64.tar.gz
[root@localhost opt]# systemctl disable --now firewalld
[root@localhost opt]# setenforce 0
#永久关闭
[root@localhost opt]# vim /etc/sysconfig/selinux
# disabled - No SELinux policy is loaded.
SELINUX=enforcing改为SELINUX=disabled
[root@localhost opt]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@localhost opt]# cd /usr/local/
[root@localhost local]# vim /etc/profile
最后一行加入
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
[root@localhost local]# source /etc/profile
[root@localhost local]# echo $PATH
/usr/local/jdk1.8.0_91/bin:/usr/local/jdk1.8.0_91/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost local]# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

[root@localhost opt]# cd /opt/
[root@localhost opt]# tar zxvf apache-tomcat-9.0.16.tar.gz 
[root@localhost opt]# mv apache-tomcat-9.0.16 /usr/local/tomcat
[root@localhost opt]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:/usr/local/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

[root@localhost opt]# netstat -ntap | grep java
tcp6 00 :::8080 :::*LISTEN2025/java 
tcp6 00 127.0.0.1:8005:::*LISTEN2025/java 
tcp6 00 :::8009 :::*LISTEN2025/java 

tomcat 120 安装nginx

[root@localhost ~]# cd /opt/
[root@localhost nginx-1.12.0]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make ##网络源
[root@localhost system]# useradd -M -s /sbin/nologin nginx
[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz 
##先传包,在解压
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.0]# cd /usr/lib/systemd/system
[root@localhost system]# vim nginx.service[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl start nginx

[root@localhost system]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_tempconffastcgi_temphtmllogsproxy_tempsbinscgi_tempuwsgi_temp

[root@localhost nginx]# cd html/
拖一张.jpg的图片
[root@localhost html]# vim test.html

<html>
<body>
<h1> jiu ni xiang tou wo
<img src="123.jpg"/>
</body>
</html>




浏览器访问http://192.168.44.120/test.
#记得关闭防火墙
[root@localhost html]# systemctl disable --now firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost html]#setenforce 0 

设置动态页面设置

#先设置110
[root@localhost webapps]# cd /usr/local/tomcat/webapps/
[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test/
[root@localhost test]# vim index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test3 page</title> 
</head>
<body>
<% out.println("动态页面 3,This is dynamic test web3");%>
</body>
</html>
[root@localhost tomcat]# cd /usr/local/tomcat/conf/
[root@localhost conf]# vim server.xml #最低端
<!-- <Host name="localhost"appBase="webapps"unpackWARs="true" autoDeploy="true">
--> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
[root@localhost test]# /usr/local/tomcat/bin/shutdown.sh 
[root@localhost test]# /usr/local/tomcat/bin/startup.sh


100
[root@localhost local]# cd /usr/local/tomcat1/webapps/
[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test/
[root@localhost test]# vim index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("动态页面 1,This is dynamic test web1");%>
</body>
</html>

[root@localhost test]# cd /usr/local/tomcat2/webapps/
[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test/
[root@localhost test]# vim index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<% out.println("动态页面 2,This is dynamic test web2");%>
</body>
</html>
[root@localhost test]# cd ..
[root@localhost webapps]# cd ..
[root@localhost tomcat2]# cd conf/
[root@localhost conf]# vim server.xml #到最底
#注释掉<!开头-->结尾Host配置添加新配置
<!-- <Host name="localhost"appBase="webapps"unpackWARs="true" autoDeploy="true">
-->  <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat2/webapps/test" path="" reloadable="true" />
[root@localhost conf]# /usr/local/tomcat2/bin/shutdown.sh#关闭
[root@localhost conf]# /usr/local/tomcat2/bin/startup.sh #启动
[root@localhost conf]# cd /usr/local/tomcat1/webapps/
[root@localhost webapps]# ls
docsexampleshost-managermanagerROOTtest
[root@localhost webapps]# cd ..
[root@localhost tomcat1]# cd conf/
[root@localhost conf]# vim server.xml 
 <!--<Host name="localhost"appBase="webapps"unpackWARs="true" autoDeploy="true">
--> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat1/webapps/test" path="" reloadable="true" />
[root@localhost conf]# /usr/local/tomcat1/bin/shutdown.sh
[root@localhost conf]# /usr/local/tomcat1/bin/startup.sh 
#Nginx 120
[root@localhost html]#cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
#在#gzipon;下添加
 #gzipon;upstream tomcat_server {server 192.168.44.100:8080 weight=1;server 192.168.44.100:8081 weight=1;server 192.168.44.110:8080 weight=1;}server {listen 80;server_namelocalhost;#charset koi8-r;#access_loglogs/host.access.logmain;  location / {root html;indexindex.html index.htm;}#添加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 ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {root /usr/local/nginx/html/img;expires 10d;}

#结束#error_page404/404.html;
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# cd ../html/
[root@localhost html]# mkdir img
[root@localhost html]# cp 123.jpg img/
[root@localhost html]# systemctl restart nginx 

测试

本机浏览器访问http://192.168.44.120/123.jpg #成功
静态页面通过 /usr/local/nginx/conf/nginx.conf 的locatin匹配到 进行访问

访问http://192.168.44.120/index.jsp
显示动态页面 1,This is dynamic test web1
刷新一下显示:
动态页面 2,This is dynamic test web2
再刷新一下显示:
动态页面 3,This is dynamic test web3
#实现反向代理和负载均衡 

四层代理

再添加准备一台Nginx服务器做web应用层:(Nginx130)192.168.44.130

添加一台nginx负载均衡服务器(Nginx140):192.168.44.140

Nginx+Tomcat负载均衡、动静分离

名称ip地址
Nginx 服务器192.168.44.120
Tomcat服务器1192.168.44.100:8080
Tomcat服务区2192.168.44.100:8081
Tomcat服务器3192.168.44.110:8080
web192.168.44.130
Nginx负载均衡192.168.44.140:8080

Nginx130安装nginx

 [root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install -y nginx
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# vim nginx.conf

打开nginx120的主配置文件复制内容到130的配置文件如下
在#36 include /etc/nginx/conf.d/*.conf;下插入
Nginx120 [root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
include /etc/nginx/conf.d/*.conf; upstream tomcat_server {server 192.168.44.100:8080 weight=1;server 192.168.44.100:8081 weight=1;server 192.168.44.110:8080 weight=1;}server {listen 80;listen [::]:80;server_name_;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf; 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 ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {root /usr/local/nginx/html/img; expires 10d;}#这里需要在Nginx130注意把路径修改一下/usr/share/nginx/html/,不然图片不显示
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl start nginx
[root@localhost conf.d]# netstat -natp |grep nginx
tcp00 0.0.0.0:800.0.0.0:* LISTEN2332/nginx: mastertcp6 00 :::80 :::*LISTEN2332/nginx: master 

Nginx120传文件给130
[root@localhost conf]# cd ..
[root@localhost nginx]# cd html/
[root@localhost html]# ls
123.jpg50x.htmlimgindex.htmltest.html
[root@localhost html]# scp -r test.html123.jpg img/root@192.168.44.130:/usr/share/nginx/html
The authenticity of host '192.168.44.130 (192.168.44.130)' can't be established.
ECDSA key fingerprint is SHA256:SY5+b7KkhALvvCVSqM6cKvjrt/o65YErcQq/1lvZvhk.
ECDSA key fingerprint is MD5:1d:e1:9b:bf:34:dc:6f:aa:e0:b2:d5:11:e1:18:92:f1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.44.130' (ECDSA) to the list of known hosts.
root@192.168.44.130's password: 
test.html 100% 8055.0KB/s 00:00123.jpg 100% 39KB13.0MB/s 00:00123.jpg 100% 39KB10.9MB/s 00:00Nginx130
[root@localhost etc]# cd /usr/share/nginx/html
[root@localhost html]# ls
123.jpg 50x.htmliconsindex.htmlpoweredby.png
404.htmlen-US imgnginx-logo.pngtest.html
[root@localhost html]# vim /etc/nginx/nginx.conf
#这里需要在Nginx130注意把路径修改一下/usr/share/nginx/html/, 
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {root /usr/share/nginx/html/;expires 10d;}


[root@localhost html]# systemctl restart nginx
主机浏览器访问http://192.168.44.130/test.html
http://192.168.44.130/index.jsp
显示成功就实现动态分离了 

Nginx140

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vim nginx.repo
#创建新文件
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@localhost yum.repos.d]# yum install -y nginx
#容
[root@localhost yum.repos.d]# vim /etc/nginx/nginx.conf 
#在http上添加
stream {upstream web_server {server 192.168.44.120:80 weight=1;server 192.168.44.130:80 weight=1; }server{listen 8080;proxy_pass web_server; }

}


http {
[root@localhost yum.repos.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost yum.repos.d]# systemctl start nginx
[root@localhost yum.repos.d]# netstat -natp | grep nginx
tcp00 0.0.0.0:800.0.0.0:* LISTEN4184/nginx: mastertcp00 0.0.0.0:80800.0.0.0:* LISTEN4184/nginx: master浏览器访问http://192.168.44.140/ 是nginx界面
访问http://192.168.44.140:8080/test.html显示文字和照片
http://192.168.239.44:140/index.jsp显示动态页面 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值