小白也看的懂的Nginx+Tomcat的负载均衡,动静分离实操
程序员文章站
2024-03-16 14:18:16
...
小白也看的懂的Nginx+Tomcat的负载均衡,动静分离实操
一:实现动静分离
1.1:实操环境
- VMware软件
- 一台centos7作为nginx代理服务器。IP地址:20.0.0.51
- 一台centos7作为Tomcat1节点服务器。IP地址:20.0.0.52
- 一台centos7作为Tomcat2节点服务器。IP地址:20.0.0.48
1.2:实验目的
- 通过访问nginx代理服务器,静态自己处理,动态交给Tomcat处理
1.3:实验步骤
1.3.1:搭建nginx服务
- 关闭防火墙
[aaa@qq.com ~]# systemctl stop firewalld.service
[aaa@qq.com ~]# setenforce 0
- 安装环境
[aaa@qq.com ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make '安装环境'
[aaa@qq.com ~]# useradd -M -s /sbin/nologin nginx '创建没有家目录,不允许登陆的nginx用户'
- 解压nginx源码包并进行编译安装
[aaa@qq.com ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[aaa@qq.com ~]# cd nginx-1.12.0/
[root@nginx nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@nginx nginx-1.12.2]# make && make install
- 创建软链接并检查语法是否正确
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@nginx nginx-1.12.2]# nginx -t
- 制作nginx启动脚本
[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx '添加使用systemctl工具进行管理'
#!/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
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@nginx nginx-1.12.2]# chkconfig --add nginx
- 启动nginx服务
[root@nginx nginx-1.12.2]# netstat -ntap |grep 80
[root@nginx nginx-1.12.2]# service nginx start
[root@nginx nginx-1.12.2]# netstat -ntap |grep 80
tcp6 0 0 :::8009 :::* LISTEN 14111/java
tcp6 0 0 :::8080 :::* LISTEN 14111/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 14111/java
1.3.2:搭建Tomcat服务
- 关闭防火墙和核心防护
[aaa@qq.com ~]# systemctl stop firewalld.service
[aaa@qq.com ~]# setenforce 0
- 解压准备的jdk
[aaa@qq.com ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
- 配置jdk环境
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# source /etc/profile '立即生效,引用环境'
- 安装Tomcat
[aaa@qq.com ~]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/ '将安装包移动到/usr/local下'
[aaa@qq.com ~]# cd /usr/local/
[aaa@qq.com local]# mv apache-tomcat-8.5.16/ tomcat '重命名'
[aaa@qq.com local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin
[aaa@qq.com local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin
[aaa@qq.com local]# startup.sh '启动Tomcat'
1.3.3:动静分离配置
- nginx上操作
- 在nginx服务器中操作编辑配置文件nginx.conf
[aaa@qq.com ~]# vim /usr/local/nginx/conf/nginx.conf
'在server中添加'
server {
..省略
'添加以下内容'
location ~.*.jsp$ { '做跳转'
proxy_pass http://20.0.0.52:8080;
proxy_set_header Host $host;
}
[aaa@qq.com ~]# nginx -t
- 创建静态页面
[aaa@qq.com ~]# 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>
[aaa@qq.com ~]# service nginx stop
[aaa@qq.com ~]# service nginx start
- tomcat上操作
- 创建动态页面
[aaa@qq.com ~]# mkdir /usr/local/tomcat/webapps/test
[aaa@qq.com ~]# 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>动态页面</div>
</body>
</html>
- 访问测试
1.3.4:nginx处理静态图片,Tomcat处理动态页面
- tomcat服务操作
[aaa@qq.com local]# vim /usr/local/tomcat/webapps/test/index.jsp
'在<div>动态页面</div>下面添加'
<img src="chiji.jpg">
- nginx服务操作
[aaa@qq.com ~]# vim /usr/local/nginx/conf/nginx.conf
'在server中添加'
server {
..省略
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root html;
expires 30d;
}
(Tomcat指路径,nginx放图片)
注意:目录名称需要和Java项目名称相同
[aaa@qq.com ~]# cd /usr/local/nginx/html/test
[aaa@qq.com test]# rz -E
rz waiting to receive.
[aaa@qq.com test]# ls
chiji.jpg
[aaa@qq.com test]# service nginx restart
- 测试
二:负载均衡
2.1:实验环境
基于上一个实验环境,还有一台centos7:20.0.0.48没有用
首先根据安装tomcat,步骤与tomcat1一样
2.2:实验目的
- 通过访问nginx代理服务器,实现负载均衡
2.3:实验步骤
- 进行页面测试
[aaa@qq.com local]# cd tomcat/
[aaa@qq.com tomcat]# mkdir -pv /web/webapp1 创建web目录
[aaa@qq.com tomcat]# vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("Welcome KEVIN Web");%>
</body>
</html>
'在Tomcat1中将KEVIN换成BENET,做一个区分'
- 修改tomcat的配置文件
vim /usr/local/tomcat/conf/server.xml
'148行后添加以下内容'
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
'注解:docBASE:web应用的文档基准目录'
'reloadable设置简史“类”是否变化'
'path=""设置默认“类”'
- 启动tomcat服务
[aaa@qq.com tomcat]# shutdown.sh
[aaa@qq.com tomcat]#startup.sh
- 测试
- 在nginx上配置动静分离,修改主配置文件
[aaa@qq.com html]# vim /usr/local/nginx/conf/nginx.conf
...省略内容
#gzip on;
'添加'
upstream tomcat_server { '添加节点服务器地址'
server 20.0.0.52:8080 weight=1;
server 20.0.0.48:8080 weight=1;
}
...省略内容
'location 里添加'
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server; ’添加,设置转发到节点服务器‘
}
- 检查语法并启动
[aaa@qq.com html]# /usr/local/nginx/sbin/nginx -t
[aaa@qq.com html]# service nginx stop
[aaa@qq.com html]# service nginx start
- 群集测试
上一篇: 负载均衡算法
下一篇: 学生信息管理系统(Java实训)