Tomcat简介
- Tomcat最初是由Sun的软件构架师詹姆斯邓肯.戴维森开发
- 安装完Tomcat后安装路径下面的目录和文件,是使用或者配置Tomcat的重要文件
Tomcat重要目录
- bin:存放启动和关闭Tomcat脚本
- conf:存放Tomcat不同的配置文件
- doc:存放Tomcat文档
- lib/japser/common:存放Tomcat运行需要的库文件
- logs:存放Tomcat执行时的LOG文件
- src:存放Tomcat的源代码
- webapps:Tomcat的主要Web发布目录
- work:存放jsp编译后产生的class文件
Nginx应用
- Nginx是一款非常优秀的HTTP服务器软件
- 支持高达50 000个并发连接数的响应
- 拥有强大的静态资源处理能力
- 运行稳定
- 内存、CPU等系统资源消耗非常低
- 目前很多大型网站都应用Nginx服务器作为后端网站程序的反向代理及负载均衡器,提升整个站点的负载并发能力
Nginx负载均衡实现原理
- Nginx实现负载均衡是通过反向代理来实现的
- 反向代理原理
Nginx配置反向代理的主要参数
- upstream服务池名{ }
- 配置后端服务器池,以提供响应数据
- proxy_pass http://服务池名
- 配置将访问请求转发给后端服务器池的服务器处理
Nginx+Tomcat负载均衡群集实验
实验环境
Nginx服务器IP地址:192.168.80.177
Tomcat1服务器IP地址:192.168.80.151
Tomcat2服务器IP地址:192.168.80.178
client测试机
搭建负载均衡
- 在Tomcat1、Tomcat2服务器上安装配置Tomcat服务(Tomcat2上网页内容为accp其他配置一样)
[[email protected] ~]# systemctl stop firewalld.service //关闭防火墙 [[email protected] ~]# mkdir /abc [[email protected] ~]# mount.cifs //192.168.100.8/LNMP-C7 /abc/ [[email protected] ~]# cd /abc/tomcat/ [[email protected] tomcat]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/ //解压JDK [[email protected] tomcat]# 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:$PATH [[email protected] tomcat]# source /etc/profile //刷新配置文件 [[email protected] tomcat]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/ //解压 [[email protected] tomcat]# cd /usr/local/ [[email protected] local]# mv apache-tomcat-8.5.16/ tomcat [[email protected] local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/ //将启动和关闭脚本便于系统识别 [[email protected] local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/ [[email protected] local]# mkdir -p /web/webapp1 //创建站点 [[email protected] local]# vim /web/webapp1/index.jsp //写入jsp网页内容 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("Welcome KGC Web");%> //输出信息 </body> </html> [[email protected] local]# vim /usr/local/tomcat/conf/server.xml //修改Tomcat配置文件 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context docBase="/web/webapp1" path="" reloadable="false"> ##添加站点信息 </Context> [[email protected] ~]# startup.sh //启动服务
- 在Nginx服务器上安装配置Nginx服务
[[email protected] ~]# systemctl stop firewalld.service ##关闭防火墙 [[email protected] ~]# setenforce 0 [[email protected] ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y ##安装环境必要组件 [[email protected] ~]# mkdir /abc [[email protected] ~]# mount.cifs //192.168.100.3/LNMP-C7 /abc/ ##挂载 Password for [email protected]//192.168.100.3/LNMP-C7: [[email protected] ~]# cd /abc/ [[email protected] abc]# tar zxvf nginx-1.12.2.tar.gz -C /usr/local/ ##解压 [[email protected] abc]# useradd -M -s /sbin/nologin nginx ##创建系统用户 [[email protected] abc]# cd /usr/local/nginx-1.12.2/ [[email protected] nginx-1.12.2]# ./configure \ ##配置 > --prefix=/usr/local/nginx > --user=nginx > --group=nginx > --with-http_stub_status_module > --with-http_gzip_static_module > --with-http_flv_module [[email protected] nginx-1.12.2]# make && make install ##编译安装 [[email protected] nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream tomcat-server { #添加地址池 server 192.168.80.151:8080 weight=1; server 192.168.80.178:8080 weight=1; } server { listen 80; .....省略 location / { root html; index index.html index.htm; proxy_pass http://tomcat-server; #添加代理,调用服务器地址池 } [[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##便于系统识别 [[email protected] nginx-1.12.2]# nginx ##开启服务
- 使用client测试机访问nginx代理服务器
动静分离
- 在Nginx服务器上修改Nginx配置文件
[[email protected] nginx-1.12.2]# vim /etc/init.d/nginx //编写service启动脚本 #!/bin/bash # chkconfig: - 99 20 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0 [[email protected] nginx-1.12.2]# chmod +x /etc/init.d/nginx [[email protected] nginx-1.12.2]# chkconfig --add nginx [[email protected] nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf server { ...省略... location ~.*.jsp$ { //匹配jsp页面跳转代理服务器池 proxy_pass http://tomcat-server; proxy_set_header Host $host; } location / { root html/test; ##修改站点 index index.html index.htm; proxy_pass http://tomcat-server; } [[email protected] nginx-1.12.2]# vim /usr/local/nginx/html/index.html //编写静态网页 <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>静态页面</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>静态页面</h1> <p>这是个静态页面</p> </body> </html> [[email protected] nginx-1.12.2]# service nginx stop ##关闭开启服务 [[email protected] nginx-1.12.2]# service nginx start
- 在Tomcat1,Tomcat2服务器上创建jsp动态页面
[[email protected] ~]# mkdir /usr/local/tomcat/webapps/test [[email protected] ~]# vim /usr/local/tomcat/webapps/test/index.jsp <!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>动态页面</title> </head> <body> <div>动态页面1</div> </body> </html> //在Tomcat2上修改为动态页面2 //访问静态 http://192.168.13.177/ //访问动态 http://192.168.13.177/test/index.jsp
- 配置Nginx处理静态图片,Tomcat处理动态页面
- 分别在两台Tomcat服务器上配置
[[email protected] ~]# vim /usr/local/tomcat/webapps/test/index.jsp <body> <div>动态页面</div><br><img src="11.jpg"> //添加页面图片 </body> [[email protected] local]# vim /usr/local/tomcat/conf/server.xml //在第149行下追加以下条目, <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="false"> </Context> [[email protected] test]# shutdown.sh //关闭重启 [[email protected] test]# startup.sh
- 在Nginx服务器上修改Nginx配置文件
[[email protected] nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { root html/test; expires 30d; } [[email protected] nginx-1.12.2]# mkdir /usr/local/nginx/html/test [[email protected] nginx-1.12.2]# cp /abc/11.jpg /usr/local/nginx/html/test/ [[email protected] html]# service nginx restart //重启服务
- 分别在两台Tomcat服务器上配置
- 使用client测试
原文地址:https://blog.51cto.com/14473285/2460409
时间: 2024-11-09 17:48:50