nginx反向代理与负载均衡

一、简介

nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。

nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。

但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储。

Http Proxy模块,功能很多,最常用的是proxy_pass和proxy_cache

如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定的URL缓存。这个集成需要在安装nginx的时候去做,如:
./configure --add-module=…/ngx_cache_purge-1.0 …

nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内

weight轮询(默认):接收到的请求按照顺序逐一分配到不同的后端服务器,即使在使用过程中,某一台后端服务器宕机,nginx会自动将该服务器剔除出队列,请求受理情况不会受到任何影响。 这种方式下,可以给不同的后端服务器设置一个权重值(weight),用于调整不同的服务器上请求的分配率;权重数据越大,被分配到请求的几率越大;该权重值,主要是针对实际工作环境中不同的后端服务器硬件配置进行调整的。

ip_hash:每个请求按照发起客户端的ip的hash结果进行匹配,这样的算法下一个固定ip地址的客户端总会访问到同一个后端服务器,这也在一定程度上解决了集群部署环境下session共享的问题。

什么是代理

说到代理,首先我们要明确一个概念,所谓代理就是一个代表、一个渠道;
此时就设计到两个角色,一个是被代理角色,一个是目标角色,被代理角色通过这个代理访问目标角色完成一些任务的过程称为代理操作过程;如同生活中的专卖店~客人到某达斯专卖店买了一双鞋,这个专卖店就是代理,被代理角色就是某达斯厂家,目标角色就是用户

正向代理

我们如果由于技术需要要去访问国外的某些网站,此时你会发现位于国外的某网站我们通过浏览器是没有办法访问的,此时大家可能都会用一个操作使用加速器进行访问,加速器的方式主要是找到一个可以访问国外网站的代理服务器,我们将请求发送给代理服务器,代理服务器去访问国外的网站,然后将访问到的数据传递给我们。

这样的代理模式称为正向代理,正向代理最大的特点是客户端非常明确要访问的服务器地址;服务器只清楚请求来自哪个代理服务器,而不清楚来自哪个具体的客户端;正向代理模式屏蔽或者隐藏了真实客户端信息。
在这里插入图片描述

反向代理

在这里插入图片描述
多个客户端给服务器发送的请求,nginx服务器接收到之后,按照一定的规则分发给了后端的业务处理服务器进行处理了。此时~请求的来源也就是客户端是明确的,但是请求具体由哪台服务器处理的并不明确了,nginx扮演的就是一个反向代理角色

反向代理,主要用于服务器集群分布式部署的情况下,反向代理隐藏了服务器的信息!

正向代理与反向代理结合

我们在实际项目操作时,正向代理和反向代理很有可能会存在在一个应用场景中,正向代理代理客户端的请求去访问目标服务器,目标服务器是一个反向代理服务器,反向代理了多台真实的业务处理服务器

二、nginx负载均衡

环境:

主机	          Ip	     安装	    系统
Nginx	192.168.100.111	 Nginx	Rockylinux9
Rs1	    192.168.100.112  Httpd	Rockylinux9
Rs2	    192.168.100.114	 Httpd	Rockylinux9
//三台主机都关闭防火墙和selinux,还需要配置好yum仓库
//nginx主机部署nginx服务,之前已经部署好了,我这里就不演示了

nginx搭建

1、rs1,rs2配置httpd测试网页

//rs1主机上,安装httpd,然后添加一个测试网页
[root@rs1 ~]# yum -y install httpd
[root@rs1 ~]# echo "this is rs1" > /var/www/html/index.html
[root@rs1 ~]# systemctl restart httpd
[root@rs1 ~]# systemctl enable httpd

//rs2主机上,安装httpd,然后添加一个测试网页
[root@rs2 ~]# yum -y install httpd
[root@rs2 ~]# echo "this is rs2" > /var/www/html/index.html
[root@rs2 ~]# systemctl restart httpd
[root@rs2 ~]# systemctl enable httpd

//node7主机测试访问
[root@node7 ~]# curl http://192.168.100.112
this is rs1
[root@node7 ~]# curl http://192.168.100.114
this is rs2

2、修改nginx配置文件,设置负载均衡

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
  #keepalive_timeout  0;
    keepalive_timeout  65;
    upstream shiqian {
        server 192.168.100.112;
        server 192.168.100.114;
    }
    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_pass http://shiqian;
        }

[root@nginx ~]# nginx -s reload

3、测试访问

[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs2
[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs2

4、配置权重

设置权重,如果想其中一台后端真实服务器,多承担一些访问量,可以去设置weight权重

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream webserver {
        server 192.168.100.112 weight=2;
        server 192.168.100.114;
    }
[root@nginx ~]# nginx -s reload

测试访问

[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs2
[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs2

此时会发现100.112主机(rs1),访问时,访问的2次后,才轮询到rs2中

5、更改端口测试访问

此时,我们发现三台主机都是使用80端口,所以,在nginx配置文件中的upstream中,对应的真实后端服务器,我们并没有设置端口。如果其中某台后端服务器使用8080端口呢?我们如何进行设置,rs1为8080端口,rs2为80端口

首先修改rs1的httpd服务,侦听8080端口,并重启httpd服务

[root@rs1 ~]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[root@rs1 ~]# systemctl restart httpd

修改nginx配置文件

    keepalive_timeout  65;
    upstream shiqian {
        server 192.168.100.112:8080 weight=2;
        server 192.168.100.114:80;
    }

测试访问

[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs2
[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs2

6、ip_hash配置

ip_hash这种负载均衡模式根据个人理解就是:例如多个用户通过nginx访问到了后端的httpd集群中,这个时候因为有不同用户,所以ip也不同,ip+hash算法计算的hash值都传到了httpd,nginx就记录了这个ip和hash值,那么下次同一个ip过来还是会分配到这个httpd的。

如果在集群中的某台服务器出现故障,我们想要从nginx的集群配置中移除掉,我们不可以直接的将那一行删掉,比如server 192.168.100.10:8080 weight=2;删掉,如果直接删掉会导致nginx的hash算法重新计算,那么用户的会话或者说缓存都会失效掉,所以这里如果不用这台服务器,直接比较为down即可,也就是 server 192.168.100.10:8080 down
这么做就可以了。

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    keepalive_timeout  65;
    upstream shiqian {
        ip_hash;
        server 192.168.100.112:8080 weight=2;
        server 192.168.100.114:80;
    }
[root@nginx ~]# nginx -s reload

验证访问

[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs1
[root@node7 ~]# curl http://192.168.100.111
this is rs1

三、动静分离nginx+tomcat

还是基于上面的环境添加一台tomcat
Tomcat:192.168.100.116

部署tomcat网页:tomcat搭建部署

测试访问以下tomcat的测试网页
tomcat测试网页

1、配置nginx,设置动静分离

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    #keepalive_timeout  0;
    keepalive_timeout  65;
    upstream shiqian {
        server 192.168.100.112;
        server 192.168.100.114;
    }
    upstream tomcat {
        server 192.168.100.116:8080;
    }

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_pass http://shiqian;
        }

        location /test {
                proxy_pass http://tomcat;
        }

[root@nginx ~]# nginx -s reload

2、测试访问

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 20
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值