简述Nginx的upstream与proxy_pass使用

Ngnix有一个很好的功能是负载均衡,将前端超高并发访问转发至后端多台服务器进行处理,可以解决单个节点压力过大,造成Web服务响应过慢,严重的情况下导致服务瘫痪,甚至无法正常提供服务的问题。
要使用此功能会用到upstream和proxy_pass
upstream 主要是配置均衡池和调度方法
proxy_pass 主要是配置代理服务器ip或服务器组的名字
演示:

三台主机:
前端:172.18.1.15
后端:172.16.0.9
后端:172.18.1.14

首先配置前端服务器

http {
    include       mime.types;
    upstream web {
    server 172.16.0.9;         
    server 172.18.1.14;
}
    default_type  application/octet-stream;

    server {
        listen       8000;
        server_name  localhost;

        location / {
         proxy_pass http://web;
        }
           } 
 }

然后,配置后端两台服务器,为便于测试,访问后台各主机时,返回各自主机对应的IP地址。

root@backupserver:~# echo " this is 172.16.0.9 " > /usr/local/nginx/html/index.html    
root@backupserver:~# curl 172.16.0.9
 this is 172.16.0.9 
root@backupserver:~# curl 172.18.1.14
this is 172.18.1.14
root@backupserver:~# 

在访问前端地址测试

[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
this is 172.18.1.14
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
this is 172.18.1.14
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
this is 172.18.1.14
[root@node03 ~]# 

测试结果可以看出:
负载均衡已配置成功,后端主机采用轮询方式工作,即第一次访问前端时,后端0.9提供代理,第二次访问前端时,后端1.14提供代理,然后再0.0,1.14 ……轮询交替进行。如果后端主机性能不一样,有的配置较高,有的已经老旧,配置较低,采用上面轮询方式显然不符合负载均衡要求。解决这个问题的办法是:采用比重的方式,在前端配置文件里加入比重参数(weight).

    upstream web {
    server 172.16.0.9 weight=3;
    server 172.18.1.14 weight=1;
}

测试结果

[root@node03 ~]# curl 172.18.1.15:8000
this is 172.18.1.14
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
this is 172.18.1.14
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# 

可以看到,此方式解决了负责均衡时,后端主机性能不同,通过比重方式处理。现在另一个问题来了,假如正访问购物网,看中一款商品,网页一刷新,就被代理到台后端另一台服务器上了,这显然不符合实际的应用要求。希望是能识别主机,也就是:当一个客户端访问前端,被代理到后台一台服务器后,以后就始终被代理到这台服务器。这里用到ip_hash 通过客户端ip进行hash,再通过hash值选择后端server

    upstream web {
    ip_hash;
    server 172.16.0.9 weight=3;
    server 172.18.1.14 weight=1;
}

结果如下,始终访问0.9服务器

[root@node03 ~]# !/usr
/usr/local/nginx/sbin/nginx -s reload
[root@node03 ~]# curl 172.18.1.15:8000                
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# curl 172.18.1.15:8000
 this is 172.16.0.9 
[root@node03 ~]# 

使用负载均衡的时候会遇到会话保持的问题,常用到的就是ip_hash

还有几个参数也是用到比较多:
1、 proxy_next_upstream error http_404 http_502;
通过这个指令,可以处理当后端服务返回404等报错时,
直接将请求转发给其他服务器,而不是把报错信息返回客户端。

2、proxy_set_header Host $host;
通过这个指令,把客户端请求的host,转发给后端。

不再演示。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿蔡BLOG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值