Nginx配置反向代理的主要参数upstream服务池名{} 配置后端服务器池,以提供响应数据 proxy_pass http://服务池名 配置将访问请求转发给后端服务器池的服务器处理
动静分离原理服务端接收来自客户端的请求中,既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端
Nginx静态处理优势Nginx处理静态页面的效率远高于Tomcat的处理能力 若Tomcat的请求量为1000次则Nainx的每秒吞吐量为3.6M Tomcat每秒的吞吐量为0.6M,Nginx的每秒吞吐量为3.6M Nginx处理静态资源的能力是Tomcat处理的6倍
配置Nginx+Tomcat负载均衡,动静分离Nginx 服务器:192.168.100.102 Tomcat 服务器1:192.168.100.103 Tomcat 服务器2:192.168.100.105:8080 192.168.100.105:8081
配置Nginx 负载均衡器[root@zzz ~]# systemctl stop firewalld[root@zzz ~]# setenforce 0[root@zzz ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make[root@zzz ~]# useradd -M -s /sbin/nologin nginx[root@zzz ~]# cd /opt[root@zzz opt]# cd nginx-1.12.2/[root@zzz nginx-1.12.2]# ./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 --with-stream 优化路径 [root@zzz nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 加入systemctl启动服务 [root@zzz nginx-1.12.2]# vim /lib/systemd/system/nginx.service 
给与权限 启动服务 [root@zzz nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service [root@zzz nginx-1.12.2]# systemctl start nginx.service [root@zzz nginx-1.12.2]# systemctl enable nginx.service
部署两台Tomcat[root@send opt]# systemctl stop firewalld.service [root@send opt]# setenforce 0[root@send opt]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/[root@send local]# mv jdk1.8.0_91/ jdk1.8[root@send jdk1.8]# vim /etc/profile 
[root@send jdk1.8]# source /etc/profile
动静分离配置(1)Tomcat1 server 配置 [root@send bin]# mkdir /usr/local/tomcat/webapps/test[root@send bin]# vim /usr/local/tomcat/webapps/test/index.jsp 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html><head><title>JSP test1 page</title> </head><body><% out.println("动态页面 1,http://www.test1.com");%></body></html> 
(2)Tomcat2 server 配置 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html><head><title>JSP test1 page </title> </head><body><% out.println("动态页面 1,http://www.test1.com");%></body></html> [root@localhost webapps]# cd ..[root@localhost tomcat8]# vim conf/server.xml 
[root@localhost local]# mkdir tomcat9/webapps/test[root@localhost local]# cp tomcat8/webapps/test/index.jsp tomcat9/webapps/test/[root@localhost local]# vim tomcat9/webapps/test/index.jsp 
[root@localhost local]# vim tomcat9/conf/server.xml 
重启服务。查看端口 
开启网页验证 


切换到Nginx服务器上 


开启网页验证 

配置动态页面请求[root@zzz conf]# vim nginx.conf 
重启服务 [root@zzz conf]# systemctl restart nginx.service 浏览器验证 
刷新。看页面跳转,是否实现负载均衡 

Nginx 负载均衡模式rr 负载均衡模式: 每个请求按时间顺序逐一分配到不同的后端服务器,如果超过了最大失败次数后(max_fails,默认1),在失效时间内(fail_timeout,默认10秒),该节点失效权重变为0,超过失效时间后,则恢复正常,或者全部节点都为down后,那么将所有节点都恢复为有效继续探测,一般来说rr可以根据权重来进行均匀分配。 least_conn 最少连接: 优先将客户端请求调度到当前连接最少的服务器。 ip_hash 负载均衡模式: 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题,但是ip_hash会造成负载不均,有的服务请求接受多,有的服务请求接受少,所以不建议采用ip_hash模式,session 共享问题可用后端服务的 session 共享代替 nginx 的 ip_hash。 fair(第三方)负载均衡模式: 按后端服务器的响应时间来分配请求,响应时间短的优先分配。 url_hash(第三方)负载均衡模式: 基于用户请求的uri做hash。和ip_hash算法类似,是对每个请求按url的hash结果分配,使每个URL定向到同一个后端服务器,但是也会造成分配不均的问题,这种模式后端服务器为缓存时比较好。 下载地址: 彻底搞懂Docker镜像分层的实现 搭建RocketMQ在本地IDEA开发调试环境教程 |