nginx动静分离和负载均衡

1.为什么要实现动静分离

1.1 nginx的处理静态资源能力超强

主要是nginx处理静态页面的效率远高于tomcat的处理能力,如果tomcat的请求量为1000次,则nginx的请求量为6000次,tomcat每秒的吞吐量为0.6M,nginx的每秒吞吐量为3.6M,可以说,nginx处理静态资源的能力是tomcat处理能力的6倍,优势可见一斑。

1.2动态资源和静态资源分开,使服务器结构更清晰。

2.动静分离原理
服务端接收来自客户端的请求中,有一部分是静态资源的请求,例如html,css,js和图片资源等等,有一部分是动态数据的请求。因为tomcat处理静态资源的速度比较慢,所以我们可以考虑把所有静态资源独立开来,交给处理静态资源更快的服务器例如nginx处理,而把动态请求交给tomcat处理。
如下图所示,我们在机器上同时安装了nginx和tomcat,把所有的静态资源都放置在nginx的webroot目录下面,把动态请求的程序都放在tomcat的webroot目录下面,当客户端访问服务端的时候,如果是静态资源的请求,就直接到nginx的webroot目录下面获取资源,如果是动态资源的请求,nginx利用反向代理的原理,把请求转发给tomcat进行处理,这样就实现了动静分离,提高了服务器处理请求的性能。

nginx 每秒连接数为0 nginx每秒处理请求_tomcat


环境说明

主机名

ip

服务

DR

192.168.152.163

nginx\

static

192.168.152.164

httpd

dynamic

192.168.152.165

httpd

在static 上配置apache

[root@static ~]# yum -y install httpd
[root@static ~]# cat /var/www/html/index.html 
Static test page
[root@static ~]# systemctl enable --now httpd

nginx 每秒连接数为0 nginx每秒处理请求_nginx_02

在dynamic上配置tomcat

安装java环境
[root@dynamic ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

下载tomcat安装包,并解压至指定目录
[root@dynamic ~]# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.tar.gz
[root@dynamic ~]# ls
apache-tomcat-9.0.37.tar.gz
[root@dynamic ~]# tar -xf apache-tomcat-9.0.37.tar.gz -C /usr/local/

创建软连接
[root@dynamic ~]# cd /usr/local/
[root@dynamic local]# ln -s apache-tomcat-9.0.37 tomcat
[root@dynamic local]# ll |grep tomcat
drwxr-xr-x  9 root root 220 Jul 31 18:31 apache-tomcat-9.0.37
lrwxrwxrwx  1 root root  20 Jul 31 18:32 tomcat -> apache-tomcat-9.0.37

在tomcat的网页目录中写一个测试页面
[root@dynamic ~]# cd /usr/local/tomcat/webapps/
[root@dynamic webapps]# mkdir test
[root@dynamic webapps]# vim test/index.jsp
//添加以下内容
<html>
<head>
        <title>test page</title>
</head>
<body>
        <%
            out.println("Dynamic test page");
        %>
</body>
</html>

启动tomcat
[root@dynamic ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@dynamic ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      1        ::ffff:127.0.0.1:8005               :::*                  
LISTEN     0      100    :::8080               :::*                  
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::*

nginx 每秒连接数为0 nginx每秒处理请求_nginx_03

在DR上配置nginx

[root@DR ~]# vim /usr/local/nginx/conf/nginx.conf
...
    upstream dynamic {
        server 192.168.152.165:8080;
    }

    upstream static {
        server 192.168.152.164;
    }
...
location / {
            proxy_pass http://static;
        }

        location ~ \.jsp$ {
            proxy_pass http://dynamic;
        }
[root@DR ~]# nginx -s reload

nginx 每秒连接数为0 nginx每秒处理请求_服务器_04


nginx 每秒连接数为0 nginx每秒处理请求_nginx_05

负载均衡

一、前言

在单一的服务器上执行WEB应用程序有一些重大的问题,当网站成功建成并开始接受大量请求时,单一服务器终究无法满足需要处理的负荷量,所以就有点显得有 点力不从心了。另外一个常见的问题是会产生单点故障,如果该服务器坏掉,那么网站就立刻无法运作了。不论是因为要有较佳的扩充性还是容错能力,我们都会想 在一台以上的服务器计算机上执行WEB应用程序。所以,这时候我们就需要用到集群这一门技术了。

在进入集群系统架构探讨之前,先定义一些专门术语:

  1. 集群(Cluster):是一组独立的计算机系统构成一个松耦合的多处理器系统,它们之间通过网络实现进程间的通信。应用程序可以通过网络共享内存进行消息传送,实现分布式计算机。
  2. 负载均衡(Load Balance):先得从集群讲起,集群就是一组连在一起的计算机,从外部看它是一个系统,各节点可以是不同的操作系统或不同硬件构成的计算机。如一个提供Web服务的集群,对外界来看是一个大Web服务器。不过集群的节点也可以单独提供服务。
  3. 特点:在现有网络结构之上,负载均衡提供了一种廉价有效的方法扩展服务器带宽和增加吞吐量,加强网络数据处理能力,提高网络的灵活性和可用性。集群系统(Cluster)主要解决下面几个问题:
    高可靠性(HA):利用集群管理软件,当主服务器故障时,备份服务器能够自动接管主服务器的工作,并及时切换过去,以实现对用户的不间断服务。
    高性能计算(HP):即充分利用集群中的每一台计算机的资源,实现复杂运算的并行处理,通常用于科学计算领域,比如基因分析,化学分析等。
    负载平衡:即把负载压力根据某种算法合理分配到集群中的每一台计算机上,以减轻主服务器的压力,降低对主服务器的硬件和软件要求。

总体来说,在负载均衡的思路下,多台服务器为对等方式,每台服务器都具有同等的地位,可以单独对外提供服务而无须其他服务器的辅助。通过负载分担技术,将外部发送来的请求按一定规则分配到对称结构中的某一台服务器上,而接收到请求的服务器都独立回应客户机的请求。

提供服务的一组服务器组成了一个应用服务器集群(cluster),集群下的对等多机环境可以增加系统的并发处理能力,和单台机器出现故障系统的错误冗余能力;同时实现了负载均衡和系统高可靠性。

实例
在static和dynamic上配置apache服务

[root@static ~]# yum -y install httpd
[root@static ~]# echo 'hello static' > /var/www/html/index.html
[root@static ~]# systemctl enable --now httpd

[root@dynamic ~]# yum -y install httpd
[root@dynamic ~]# echo 'hello dynamic' > /var/www/html/index.html
[root@dynamic ~]# systemctl enable --now httpd

在DR上编辑配置文件

[root@DR ~]# vim /usr/local/nginx/conf/nginx.conf
...
//在http段下面添加
upstream runtime.com {
        server 192.168.152.164 weight=2;
        server 192.168.152.165 weight=1;
    }
...
location / {
            proxy_pass http://runtime.com; 
        }
...
[root@DR ~]# nginx -s reload
[root@node01-Linux ~]# curl 192.168.152.163
hello static
[root@node01-Linux ~]# curl 192.168.152.163
hello dynamic
[root@node01-Linux ~]# curl 192.168.152.163
hello static
[root@node01-Linux ~]# curl 192.168.152.163
hello static
[root@node01-Linux ~]# curl 192.168.152.163
hello dynamic
[root@node01-Linux ~]# curl 192.168.152.163
hello static
[root@node01-Linux ~]# curl 192.168.152.163
hello static