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

大鹏一日同风起,扶摇直上九万里。这篇文章主要讲述Nginx+Tomcat负载均衡及动静分离相关的知识,希望能为你提供帮助。
nginx+Tomcat负载均衡及动静分离 1.Nginx负载均衡实现原理1.Nginx实现负载均衡是通过反向代理实现
2.反向代理原理
3.Nginx 配置反向代理的主要参数

  • upstream 服务池名
    配置后端服务器池,比提供相应数据
  • proxy_passhttp://服务池名
    配置将访问请求转发给后端服务器池的服务器处理
2.Nginx动静分离实现原理 1.动静分离原理
服务端接收来自客户端的请求中。既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端
Nginx+Tomcat负载均衡及动静分离

文章图片

2 Nginx 静态处理优势
【Nginx+Tomcat负载均衡及动静分离】? Nginx处理静态页面的效率远高于Tomcat的处理能力
? 若Tomcat的请求量为1000次,则Nginx的请求量为6000次
? Tomcat每秒的吞吐量为0.6M,Nginx的每秒吞吐量为3.6M
? Nginx处理静态资源的能力是Tomcat处理的6倍
1.准备三台服务器,Nginx作为负载均衡器,Tomcat作为应用服务器
Nginx 服务器:192.168.19.11:80 Tomcat服务器1:192.168.19.19:8080 Tomcat服务器2:192.168.19.17:8080192.168.19.17:8081

2.部署Nginx 负载均衡器
systemctl stop firewalld setenforce 0yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ makeuseradd -M -s /sbin/nologin nginx

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

文章图片

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

文章图片

cd /opt tar zxvf nginx-1.12.0.tar.gz -C /opt/cd nginx-1.12.0/ ./configure \\ --prefix=/usr/local/nginx \\ --user=nginx \\ --group=nginx \\ --with-file-aio \\#启用文件修改支持 --with-http_stub_status_module \\#启用状态统计 --with-http_gzip_static_module \\#启用 gzip静态压缩 --with-http_flv_module \\#启用 flv模块,提供对 flv 视频的伪流支持 --with-http_ssl_module#启用 SSL模块,提供SSL加密功能 ---------------------------------------------------------------------------------------------------------- ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module

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

文章图片

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

文章图片

make & & make installln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecrReload=/bin/kill -s HUP $MAINPID ExecrStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.targetchmod 754 /lib/systemd/system/nginx.service systemctl start nginx.service systemctl enable nginx.service

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

文章图片

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

文章图片

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

文章图片

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

文章图片

3.部署2台Tomcat 应用服务器
cd /opt rpm -qpl jdk-8u201-linux-x64.rpm rpm -ivh jdk-8u201-linux-x64.rpm java -versionsystemctl stop firewalld systemctl disable firewalld setenforce 0tar zxvf apache-tomcat-9.0.16.tar.gz mkdir /usr/local/tomcat mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1 cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2vim /etc/profile.d/tomcat.sh #tomcat1 export CATALINA_HOME1=/usr/local/tomcat/tomcat1 export CATALINA_BASE1=/usr/local/tomcat/tomcat1 export TOMCAT_HOME1=/usr/local/tomcat/tomcat1#tomcat2 export CATALINA_HOME2=/usr/local/tomcat/tomcat2 export CATALINA_BASE2=/usr/local/tomcat/tomcat2 export TOMCAT_HOME2=/usr/local/tomcat/tomcat2 ------------------------------ source /etc/profile.d/tomcat.shvim /usr/local/tomcat/tomcat2/conf/server.xml < Server port="8006" shutdown="SHUTDOWN"> #22行,修改Server prot,默认为8005 -> 修改为8006 < Connector port="8081" protocol="HTTP/1.1"#69行,修改Connector port,HTTP/1.1默认为8080 -> 修改为8081 < Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> #116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010 ------------------------------ 第一个连接器默认监听8080端口,负责建立HTTP连接。在通过浏览器访问Tomcat服务器的Web应用时,使用的就是这个连接器。第二个连接器默认监听8009端口,负责和其他的HTTP服务器建立连接。在把Tomcat与其他HTTP服务器集成时,就需要用到这个连接器。 ------------------------------vim /usr/local/tomcat/tomcat1/bin/startup.sh #------------------------------ # Start Script for the CATALINA Server #------------------------------ ##添加以下内容 export CATALINA_BASE=$CATALINA_BASE1 export CATALINA_HOME=$CATALINA_HOME1 export TOMCAT_HOME=$TOMCAT_HOME1vim /usr/local/tomcat/tomcat1/bin/shutdown.sh #------------------------------ # Stop script for the CATALINA Server #------------------------------ export CATALINA_BASE=$CATALINA_BASE1 export CATALINA_HOME=$CATALINA_HOME1 export TOMCAT_HOME=$TOMCAT_HOME1vim /usr/local/tomcat/tomcat2/bin/startup.sh #------------------------------ # Start Script for the CATALINA Server #------------------------------ export CATALINA_BASE=$CATALINA_BASE2 export CATALINA_HOME=$CATALINA_HOME2 export TOMCAT_HOME=$TOMCAT_HOME2vim /usr/local/tomcat/tomcat2/bin/shutdown.sh #------------------------------ # Stop script for the CATALINA Server #------------------------------ export CATALINA_BASE=$CATALINA_BASE2 export CATALINA_HOME=$CATALINA_HOME2 export TOMCAT_HOME=$TOMCAT_HOME2/usr/local/tomcat/tomcat1/bin/startup.sh /usr/local/tomcat/tomcat2/bin/startup.sh netstat -natp | grep java

4.单独部署1台Tomcat 应用服务器
#!/bin/bash #关闭防火墙 systemctl stop firewalld systemctl disable firewalld setenforce 0#安装JDK cd /opt rpm -qpl jdk-8u201-linux-x64.rpm rpm -ivh jdk-8u201-linux-x64.rpm java -version#设置JDK环境变量 echo export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64 export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar export PATH=$JAVA_HOME/bin:$PATH > /etc/profile.d/java.shsource /etc/profile#安装启动Tomcat cd /opt tar zxvf apache-tomcat-9.0.16.tar.gz mv apache-tomcat-9.0.16 /usr/local/tomcat/usr/local/tomcat/bin/startup.shsource tomcat.sh netstat -natp | grep 8080 java -version

3.动静分离配置Tomcat Server1 配置
mkdir /usr/local/tomcat/webapps/pxlvim /usr/local/tomcat/webapps/pxl/index.jsp< %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> < html> < head> < title> JSP test2 page< /title> < /head> < body> < % out.println("动态页面:无情老田在线跳水"); %> < /body> < /html> vim /usr/local/tomcat/conf/server.xml < Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> < Context docBase="/usr/local/tomcat/webapps/lic" path="" reloadable="true" /> < /Host> /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh

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

文章图片

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

文章图片

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

文章图片

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

文章图片

Tomcat Server2 配置
mkdir /usr/local/tomcat/tomcat1/webapps/test /usr/local/tomcat/tomcat2/webapps/testvim /usr/local/tomcat/tomcat1/webapps/test/index.jsp < %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> < html> < head> < title> JSP test2 page< /title> #指定为 test2 页面 < /head> < body> < % out.println("动态页面 2,http://www.test2.com"); %> < /body> < /html> vim /usr/local/tomcat/tomcat1/conf/server.xml #删除前面的 HOST 配置 < Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> < Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" /> < /Host> /usr/local/tomcat/tomcat1/bin/shutdown.sh /usr/local/tomcat/tomcat1/bin/startup.sh vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp < %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> < html> < head> < title> JSP test3 page< /title> #指定为 test3 页面 < /head> < body> < % out.println("动态页面 3,http://www.test3.com"); %> < /body> < /html> vim /usr/local/tomcat/tomcat2/conf/server.xml #删除前面的 HOST 配置 < Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> < Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" /> < /Host> /usr/local/tomcat/tomcat2/bin/shutdown.sh /usr/local/tomcat/tomcat2/bin/startup.sh

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

文章图片

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

文章图片

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

文章图片

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

文章图片

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

文章图片

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

文章图片

Nginx server 配置
#准备静态页面和静态图片 echo < html> < body> 这是静态页面< /body> < /html> > /usr/local/nginx/html/index.html mkdir /usr/local/nginx/html/pxl cp /root/game.jpg /usr/local/nginx/html/imgvim /usr/local/nginx/conf/nginx.conf ...... http ...... #gzip on; #配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大 upstream tomcat_server server 192.168.19.19:8080 weight=1; server 192.168.19.17:8080 weight=1; server 192.168.19.17:8081 weight=1; server listen 80; server_name www.kgc.com; charset utf-8; #access_log logs/host.access.log main; #配置Nginx处理动态页面请求,将 .jsp文件请求转发到Tomcat 服务器处理 location ~ .*\\.jsp$ proxy_pass http://tomcat_server; #设置后端的Web服务器可以获取远程客户端的真实IP ##设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认HOST的值为proxy_pass指令设置的主机名。如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来在反向代理服务器,如果后端有防攻ji策略的话,那么机器就被封掉了。 proxy_set_header HOST $host; ##把$remote_addr赋值给X-Real-IP,来获取源IP proxy_set_header X-Real-IP $remote_addr; ##在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #配置Nginx处理静态图片请求 location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf|css)$ root /usr/local/nginx/html/img; expires 10d; location / root html; index index.html index.htm; ............

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

文章图片

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

文章图片

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

文章图片

4.测试效果
测试静态页面效果
浏览器访问http://192.168.19.11/
浏览器访问 http://192.168.19.11/error.jpg
Nginx+Tomcat负载均衡及动静分离

文章图片

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

文章图片

测试负载均衡效果,不断刷新浏览器测试
浏览器访问http://192.168.19.19/test/index.jsp
Nginx+Tomcat负载均衡及动静分离

文章图片

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

文章图片

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

文章图片


    推荐阅读