ngixn+tomcat负载均衡 动静分离配置 (nginx反向代理)

Tomcat主要配置文件

  1. bin:存放启动和关闭Tomcat脚本
  2. conf:存放Tomcat不同的配置文件
  3. doc:存放Tomcat文档
  4. lib:存放Tomcat运行需要的库文件
  5. logs:存放Tomcat执行时的LOG文件
  6. src:存放Tomcat的源代码
  7. webapps:Tomcat的主要web发布目录
  8. work:Tom存放jsp编译后产生的

Nginx负载均衡实现原理

  • Nginx配置反向代理的主要参数
  1. upstream 服务器池名{ }

    配置后端服务器池,以提供响应数据

  2. proxy_pass http://服务池名

    配置将访问请求转发给后端服务器池的服务器处理

  • 动静分离原理

    服务端接受来自客户端的请求中,既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端

  • Nginx静态处理优势

  1. Nginx处理静态页面的效率远高于Tomcat的处理能力
  2. 若Tomcat的请求量为1000次,则Nginx的请求量为6000次
  3. Tomcat每秒的吞吐量为0.6M,Nginx的每秒吞吐量为3.6M
  4. Nginx处理静态资源的能力是Tomcat处理的6倍

实验环境

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

服务机台数3:1nginx,2tomcat

服务器IP
Nginx负载均衡器192.168.136.88
Tomcat-Webserver1192.168.136.60
Tomcat-Webserver2192.168.136.10

nginx服务安装与部署

重新命名

[root@localhost ~]# hostnamectl set-hostname nginx
[root@nginx ~]# 

所有服务器关闭防护墙

[root@nginx init.d]# iptables -F
[root@nginx init.d]# setenforce 0

配置nginx

[root@nginx ~]# yum -y install gcc-c++ gcc pcre-devel zlib-devel
[root@nginx ~]#useradd  -M -s /sbin/nologin nginx
[root@nginx ~]#cd nginx-1.12.0/
[root@nginx nginx-1.12.2]#./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[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]# cd /etc/init.d/
[root@nginx init.d]# vim nginx
#!/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 init.d]# chmod +x nginx 
[root@nginx init.d]# chkconfig --add nginx 
[root@nginx init.d]# service nginx start 
[root@nginx init.d]# netstat -ntap | grep nginx 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15334/nginx: master 

部署tomcat服务器

[root@tomcat ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@tomcat ~]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/
设置环境变量
[root@tomcat local]# vim /etc/profile
在末尾插入
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:$JAVA_HOME/lib:${JAVA_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
[root@tomcat local]# cd /usr/local/
[root@tomcat local]# source /etc/profile
[root@tomcat local]# mv apache-tomcat-8.5.16/ tomcat
系统识别
[root@tomcat local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@tomcat local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@tomcat02 local]# startup.sh 
[root@localhost local]# netstat -ntap | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      22407/java  

动静分离配置

在nginx中配置静态页面 (请求java转发到tomcat处理)

vim /usr/local/nginx/conf/nginx.conf
在42行下添加
 42         location ~.*.jsp$ {
 43            proxy_pass http://192.168.136.40:8080;   
 44            proxy_set_header Host $host;
 45  } 

检查格式

[root@nginx init.d]# 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

创建静态页面

 cd /usr/local/nginx/html/     站点目录

创建静态页面

vim index.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 >
     </style>
     </head>
      <body>
       <h1>静态页面</h1>
       <p>这是个静态页面</P>
 </body>
 </html >

ps:可以不行这么多中间 比如(style美好页面功能,width宽度,margin:0 auto外边距,head 头部,body详细内容)

开启服务

[root@nginx html]# service nginx stop 
[root@nginx html]# service nginx start

配置动态页面

在Tomcat中配置

进入站点

cd /usr/local/tomcat/webapps/

创建动态页面

[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test/
[root@localhost test]# vim 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>

开启服务

[root@tomcat test]# shutdown.sh 

[root@tomcat test]# startup.sh 

查看一下静态页面结果

在这里插入图片描述

查看一下动态页面结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xBzoOqA9-1599400446160)(C:\Users\19437\AppData\Roaming\Typora\typora-user-images\image-20200904104118342.png)]

图片动静分离

nginx处理静态图片,tmocat处理动态页面

在tomcat上加上标签

[root@tomcat test]# cd /usr/local/tomcat/webapps/test/
[root@tomcat test]# vim index.jsp
 13 <img src="cat.jpg"/>

nginx上面更改

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
在43行插入
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
 43                root html;
 44                expires 30d;
 45         }  

ps:运用正则表达式,去站点寻找,缓存30天

加入图片,加入时要注意图片的目录名称要和java项目名称一致,webapps是项目,test是项目名称

[root@nginx html]# cd /usr/local/nginx/html/
[root@nginx html]# mkdir test
[root@nginx html]# cd test/
[root@nginx test]# rz -E    输出一张图到这里
[root@nginx test]# ls
cat.jpg

开启服务

[root@nginx test]# service nginx stop 
[root@nginx test]# service nginx start 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值