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

tech2023-02-09  106

文章目录

Tomcat重要目录Nginx负载均衡原理Nginx动静分离原理实验环境Nginx安装配置负载均衡Tomcat安装配置轮询动静分离配置

Tomcat重要目录

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

Nginx负载均衡原理

Nginx是一款非常优秀的HTTP服务器软件

支持高达50000个并发连接数的响应拥有强大的静态资源处理能力内存、CPU等系统资源消耗非常低·运行稳定

Nginx实现负载均衡是通过反向代理实现 反向代理原理 Nginx配置反向代理的主要参数

upstream服务池名{} 配置后端服务器池,以提供响应数据 proxy_pass http://服务池名 配置将访问请求转发给后端服务器池的服务器处理

Nginx动静分离原理

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

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

实验环境

一台Nginx IP192.168.110.10 二台Tomcat IP192.168.110.15/20

Nginx安装配置负载均衡

安装Nginx服务

[root@nginx opt]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel [root@nginx opt]# useradd -M -s /sbin/nologin nginx [root@nginx opt]# tar zxvf nginx-1.12.2.tar.gz -C /usr/local/ [root@nginx opt]# cd /usr/local/nginx-1.12.2/ [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.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin [root@nginx nginx-1.12.2]# 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 [root@nginx nginx-1.12.2]# vim /etc/init.d/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 nginx-1.12.2]# chmod +x /etc/init.d/nginx [root@nginx nginx-1.12.2]# service nginx start [root@nginx nginx-1.12.2]# netstat -natp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22832/nginx: master

编辑配置文件实现负载均衡

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf #gzip on; #找到这行,在下面添加 upstream tom { #设置节点服务器,名字随意 server 192.168.110.15:8080 weight=1; ##指向cat1 IP,weight指的是权重 server 192.168.110.20:8080 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location ~.*.jsp$ { #往下加,匹配到以 jsp 结尾的网页再两台节点服务器中轮询 proxy_pass http://tom; } [root@nginx nginx-1.12.2]# systemctl stop firewalld [root@nginx nginx-1.12.2]# setenforce 0

Tomcat安装配置轮询

两台通样操作

[root@cat1 opt]# tar zxvf jdk-8u91-linux-x64.tar.gz [root@cat1 opt]# mv jdk1.8.0_91/ /usr/local/ [root@cat1 opt]# 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 [root@cat1 opt]# source /etc/profile [root@cat1 opt]# java -version java version "1.8.0_91" Java(TM) SE Runtime Environment (build 1.8.0_91-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode) [root@cat1 opt]# tar zxvf apache-tomcat-8.5.16.tar.gz [root@cat1 opt]# mv apache-tomcat-8.5.16 /usr/local/tomcat [root@cat1 opt]# cd /usr/local/tomcat/ [root@cat1 tomcat]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin [root@cat1 tomcat]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin [root@cat1 opt]# mkdir -p /web/tom1 [root@cat1 opt]# vim /web/tom1/index.jsp #tom2改成 /web/tom2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>tom1</title> </head> <body> <% out.println("tom1.");%> #另一台配置 tom2 </body> </html> [root@cat1 opt]# vim /usr/local/tomcat/conf/server.xml 148 <Host name="localhost" appBase="webapps" 149 unpackWARs="true" autoDeploy="true"> #添加 150 <Context docBase="/web/cat01" path="" reloadable="false"> 151 </Context> [root@cat1 opt]# startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_91/jre Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@cat1 opt]# netstat -ntap |grep 8080 tcp6 0 0 :::8080 :::* LISTEN 74552/java

动静分离配置

静态页面nginx,动态页面路径指tomcat,

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name localhost; location ~.*.jsp$ { proxy_pass http://192.168.110.15:8080; #指向Tomcat服务器地址 proxy_set_header Host $host; } [root@nginx nginx-1.12.2]# cd /usr/local/nginx/html/ [root@nginx html]# vim index.html <!DOCTYPE html> <html> <head> <title>静态页面</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"> #设置支持中文字符集 <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>静态页面</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>静态页面</em></p> </body> </html> [root@nginx html]# service nginx stop [root@nginx html]# service nginx start

Tomcat服务器添加动态网页

[root@cat01 bao]# vim /web/tom1/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="nginx/html; charset=UTF-8"> ##位置和图的位置一样,nginx/html <title>动态页面</title> </head> <body> <div>这是一个动态页面.</div><br> </body> </html>

nginx处理静态图片,Tomcat处理动态页面配置

Tomcat配置 <body> <div>这是一个动态页面.</div><br> <img src='c.png'> #添加图片名称 </body> ngin配置 [root@nginx html]# vim /usr/local/nginx/conf/nginx.conf location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { #添加此段内容,匹配这些类型的文件 root html; #html站点目录 expires 30d; #缓存时间30天 } [root@nginx htmll]# ls #添加图片 50x.html c.png index.html

最新回复(0)