准备环境:三台主机:一台nginx:172.18.9.200、两台Tomcat:172.18.9.201;172.18.9.202.
一、安装Nginx、Tomcat:
[root@200 ~]#wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #更新镜像
[root@200 ~]#yum -y install epel-release
[root@200 ~]#yum install nginx
[root@201/202 src]#ll
total 172028
-rw-r--r-- 1 root root 176154027 Nov 20 03:06 jdk-8u191-linux-x64.rpm
[root@201/202 src]#yum -y install jdk-8u191-linux-x64.rpm
[root@201/02 src]#yum install -y tomcat.noarch tomcat-admin-webapps.noarch tomcat-docs-webapp.noarch tomcat-webapps.noarch tomcat-javadoc.noarch
二、配置Nginx:
[root@200 ~]#vim /etc/nginx/conf.d/tomcat.conf
server {
listen 80;
server_name www.ready.com;
location / {
proxy_pass http://172.18.9.201:8080;
}
}
三、查看Tomcat的默认配置:
[root@201 /etc/tomcat]#ls
Catalina catalina.properties context.xml logging.properties tomcat.conf web.xml
catalina.policy conf.d log4j.properties server.xml tomcat-users.xml
四、使用Tomcat默认配置测试Nginx+Tomcat是否连通:
启动Nginx、Tomcat:
[root@200 ~]#systemctl start nginx
[root@201 tomcat]#systemctl start tomcat
五、访问www.ready.com:
六、在Tomcat上部署测试应用:
1、以WAR包为例,创建一个动态网页:
a、创建项目目录:
[root@201 tomcat]#mkdir -pv /var/lib/tomcat/webapps/test/{WEB-INF,META-INF,classes,lib}
mkdir: created directory ‘/var/lib/tomcat/webapps/test’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/WEB-INF’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/META-INF’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/classes’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/lib’
b、创建主页文件index.jsp(功能为查询session状态):
[root@201 tomcat]#vim /var/lib/tomcat/webapps/test/index.jsp
<%@ page language="java" %>
Tomcat.ready.com
Session ID | <%= session.getId() %> |
Created on | <%= session.getCreationTime() %> |
c、访问http://www.ready.com/test/index.jsp
七、Nginx做反向代理负载均衡后端Tomcat:
反向代理:反向代理也叫reverse proxy,指的是代理外网用户的请求到内部的指定web服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。
1、在主机3上安装Tomcat,并修改index.jsp文件:
[root@202 src]#mkdir -pv /var/lib/tomcat/webapps/test/{WEB-INF,META-INF,classes,lib}
mkdir: created directory ‘/var/lib/tomcat/webapps/test’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/WEB-INF’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/META-INF’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/classes’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/lib’
[root@202 src]#vim /var/lib/tomcat/webapps/test/index.jsp
<%@ page language="java" %>
Jerrymouse.ready.com
#修改标题颜色和内容Session ID | <%= session.getId() %> |
Created on | <%= session.getCreationTime() %> |
2、在172.18.9.200上配置Nginx,在默认配置中的http模块中插入upstream:
nginx通过proxy_pass_http 配置代理站点,upstream实现负载均衡。
[root@200 ~]#vim /etc/nginx/nginx.conf
upstream tcsv {
server 172.18.9.201:8080;
server 172.18.9.202:8080;
}
[root@200 ~]#vim /etc/nginx/conf.d/tomcat.conf
server {
listen 80;
server_name www.ready.com;
location / {
proxy_pass http://tcsv;
}
}
3、重启Nginx和Tomcat,再次访问www.ready.com:
[root@200 ~]#systemctl restart nginx
[root@201/202 tomcat]#systemctl restart tomcat
本实验结束!