2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > ngixn+tomcat负载均衡 动静分离配置 (nginx反向代理)

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

时间:2023-05-22 11:40:20

相关推荐

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

文章目录

Tomcat主要配置文件Nginx负载均衡实现原理实验环境动静分离配置创建静态页面配置动态页面图片动静分离

Tomcat主要配置文件

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

Nginx负载均衡实现原理

Nginx配置反向代理的主要参数

upstream 服务器池名{ }

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

proxy_pass http://服务池名

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

动静分离原理

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

Nginx静态处理优势

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

实验环境

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

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

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 ScriptPROG="/usr/local/nginx/sbin/nginx"PIDF="/usr/local/nginx/logs/nginx.pid"case "$1" instart)$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 1esacexit 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 00 0.0.0.0:80 0.0.0.0:*LISTEN15334/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_91export JRE_HOME=${JAVA_HOME}/jreexport CLASSPATH=.:$JAVA_HOME/lib:${JAVA_HOME}/libexport 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 8080tcp6 00 :::8080 :::*LISTEN22407/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 -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: 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" "/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

查看一下静态页面结果

查看一下动态页面结果

图片动静分离

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

在tomcat上加上标签

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

nginx上面更改

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf在43行插入location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {43root html;44expires 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]# lscat.jpg

开启服务

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。