nginx的反向代理及负载均衡配置

nginx的简介

nginx反向代理,也称为网关服务器,用户只需要访问 代理网关服务器,由代理请求访问 上游真实服务器,然后真实服务器返回结果,再由代理返回给客户。这样一般 代理服务器有2个网卡,一个公网一个私网,上游真实服务器与代理的私网网络规划同一网段,实现内部通信。公网网卡拥有一个公网IP提供的客户访问,这样既节省了 公网IP的地址的申请与使用,同时 真实服务器工作在私网段被隐藏起来变得更安全。

相关模块:
http_proxy_module 和 http_upstream_module 模块

实验环境:

192.168.121.164 运行 nginx 配置代理功能, 实现将请求根据代理算法 负载到上游服务器的某台
192.168.121.172 运行 8080 8081 8082 8083 四个web 相当于 总共运行有4 台web server

由于懒得创建四个虚拟机,所以用四个端口来代替也是一样的效果

实验要求:

实现8080 8081 8082 8083 台web server 运行 web项目,配置代理 E 代理服务器实现以下需求:
一:8080 8081 负载 “images” 目录下的资源的请求
二:8082 8083 负载 “sound” 目录下的资源的请求
三:8080 8081 8082 8083 共同负载其他资源的请求

四如果某类资源(images sound 其他)服务器都不可用时,则代理服务器器自己作为 backup 负载

开始操作:

要确定自己的nginx服务支持http_proxy_module 和 http_upstream_module 模块

可以用nginx -V命令 查看自己的服务器是否包含了这两个模块

首先根据要求先配置好代理服务器的配置

在html服务下配置好上游服务器的规则,这是代理服务器中的配置,加在server服务配置前就好了

upstream webServer {  #要求三的相关配置
       #指定负载均衡的算法,默认为random轮询,如果不指定weight则可能随机,加weight则按比重分配
       #ip_hash;  ---指定特定的负载均衡算法
       #server  指定上游真实服务器
server  192.168.121.164:8090  backup;   #要求四的配置
server  192.168.121.172:8080 weight=2  max_fails=3  fail_timeout=10;
server  192.168.121.172:8081 weight=1  max_fails=3  fail_timeout=10;
server  192.168.121.172:8082 weight=2  max_fails=3  fail_timeout=10;
server  192.168.121.172:8083 weight=1  max_fails=3  fail_timeout=10;
 }
upstream imageserver {   #要求一的
server  192.168.121.172:8080 weight=1  max_fails=3  fail_timeout=10;
server  192.168.121.172:8081 weight=1  max_fails=3  fail_timeout=10;
}

upstream soundserver {   #要求二的
server  192.168.121.172:8082 weight=1  max_fails=3  fail_timeout=10;
server  192.168.121.172:8083 weight=1  max_fails=3  fail_timeout=10;
}

然后是代理服务器server的配置

    server {
        listen       80;
        proxy_buffering on;
        proxy_buffer_size  128k;
        proxy_buffers      32  16k;
        proxy_send_timeout  30s;
        proxy_read_timeout  30s;
        proxy_connect_timeout   5s;
        #匹配要求一
        location /images/ {
            proxy_pass   http://imageserver;
            proxy_set_header Host    $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
        #匹配要求二
        location /sound/ {
            proxy_pass   http://soundserver;
            proxy_set_header Host    $host;
            proxy_set_header X-Real-IP $remote_addr;
             }

		匹配要求三
        location / {
        proxy_pass   http://webServer;
        proxy_set_header Host    $host;
        proxy_set_header X-Real-IP $remote_addr;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

然后代理服务器的配置就好了

然后就可以去配置上游服务器(也就是后端服务器)

首先规划好目录,如下

[root@server172 ~]# tree /www/
/www/
├── 8080
│   ├── images
│   │   └── index.html
│   └── index.html
├── 8081
│   ├── images
│   │   └── index.html
│   └── index.html
├── 8082
│   ├── index.html
│   └── sound
│       └── index.html
└── 8083
    ├── index.html
    └── sound
        └── index.html

8 directories, 8 files


然后要在每个服务下的文件里创建好对应的标识,如下我只是随便输入一点可以标识我们访问的哪个web服务页面的信息的而已

满足要求三的配置

[root@server172 ~]# cat /www/8080/index.html 
this is 8080.com
[root@server172 ~]# cat /www/8081/index.html 
this is 8081.com
[root@server172 ~]# cat /www/8082/index.html 
this is 8082.com
[root@server172 ~]# cat /www/8083/index.html 
this is 8083.com

满足要求一和要求二的配置

[root@server172 ~]# cat /www/8080/images/index.html 
this is 8080.images
[root@server172 ~]# cat /www/8081/images/index.html 
this is 8081.images
[root@server172 ~]# cat /www/8082/sound/index.html 
this is 8082.sound
[root@server172 ~]# cat /www/8083/sound/index.html 
this is 8083.sound

然后去配置我们后端服务器server的配置

server {
    listen      8080;
    server_name  www.abc.com;
    location / {
        root   /www/8080;
        index  index.html index.htm;
    }
    access_log  logs/access.log  main;
}
server {
    listen      8081;
    server_name  www.abc.com;
    location / {
        root   /www/8081;
        index  index.html index.htm;
    }
    access_log  logs/access.log  main;
}
server {
    listen      8082;
    server_name  www.abc.com;
    location / {
        root   /www/8082;
        index  index.html index.htm;
    }
    access_log  logs/access.log  main;
}
server {
    listen      8083;
    server_name  www.abc.com;
    location / {
        root   /www/8083;
        index  index.html index.htm;
    }
    access_log  logs/access.log  main;
}
          

然后所有的配置就完成了

重启我们的nginx后就可以去网页访问了

下面我为了方便直接用curl来测试了,结果是一样的

可以看到我们的每台服务器都分配到了请求,负载均衡就是通过这样实现的

至于访问的次数不一样是根据算法和权重来决定的,可以看的我们在代理服务器上给80和82端口的权重是2,而81和83的权重是1

[root@ly001 ~]# curl 192.168.121.164
this is 8082.com
[root@ly001 ~]# curl 192.168.121.164
this is 8080.com
[root@ly001 ~]# curl 192.168.121.164
this is 8082.com
[root@ly001 ~]# curl 192.168.121.164
this is 8081.com
[root@ly001 ~]# curl 192.168.121.164
this is 8083.com
[root@ly001 ~]# curl 192.168.121.164
this is 8080.com
[root@ly001 ~]# curl 192.168.121.164
this is 8082.com

要求一和要求二的

[root@ly001 ~]# curl 192.168.121.164/sound/
this is 8083.sound
[root@ly001 ~]# curl 192.168.121.164/sound/
this is 8082.sound
[root@ly001 ~]# curl 192.168.121.164/sound/
this is 8083.sound
[root@ly001 ~]# curl 192.168.121.164/sound/
this is 8082.sound
[root@ly001 ~]# curl 192.168.121.164/images/
this is 8080.images
[root@ly001 ~]# curl 192.168.121.164/images/
this is 8081.images
[root@ly001 ~]# curl 192.168.121.164/images/
this is 8080.images
[root@ly001 ~]# curl 192.168.121.164/images/
this is 8081.images

可以看的测试的结果是一样的。

反向代理的配置到这里就完成了,原理就是这样的

写这篇文章的时候我也有参考一些其它文章,如有侵权请告知删除。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值