前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx实现负载均衡

nginx实现负载均衡

作者头像
仙士可
发布2020-05-25 16:07:43
9860
发布2020-05-25 16:07:43
举报
文章被收录于专栏:仙士可博客仙士可博客

proxy_pass、upstream与resolver

upstream

upstream将创建一个上游服务配置项,用于交给proxy_pass 转发ip.

代码语言:javascript
复制
    upstream x.cn {
        server 192.168.192.134:80;
    }

只有当proxy_passs调用时,upstream才会生效:

代码语言:javascript
复制
upstream x.cn {
     server 192.168.192.134:80;
}

server
{
    listen 80;
    server_name 1.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/1.cn;
    
	 location / {
            proxy_pass http://x.cn;
     }
}

当访问服务器1.cn时,读取 proxy_pass代理配置,获取到upstream ip,将请求转发到 192.168.192.134

host为x.cn

proxy_pass反向代理

通过proxy_pass,可将请求代理到指定服务中,例如:

代码语言:javascript
复制
 server {
        listen       80;
        server_name  localhost;

        location /proxy {
            proxy_pass http://x.cn;
        }
     }

当访问localhost/proxy时,nginx将会自动解析x.cn ip,将请求代理到 解析后的ip中.

当x.cn无法解析或未设置dns服务器时,将会报错

代码语言:javascript
复制
nginx: [emerg] host not found in upstream "x.cn" in /www/server/panel/vhost/nginx/1.cn.conf:9

当没有设置upstream时,proxy_pass将通过dns服务器解析ip,默认添加一个upstream ip,用于实现转发请求.

反向代理配置项:

代码语言:javascript
复制
      #代理配置参数
        proxy_connect_timeout 180;
        proxy_send_timeout 180;
        proxy_read_timeout 180;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarder-For $remote_addr;

resolve

代码语言:javascript
复制
upstream 1.cn {
     server 192.168.192.134:80;
}

server
{
    listen 80;
    server_name 1.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/1.cn;
    resolver 114.114.114.114;
	location / {
     	    set $testCn 1.cn;
            proxy_pass http://$testCn;
     }
}

当设置resolve后, nginx将会忽略本身设置的dns,本机的hosts,直接通过resolve的dns服务器动态获取ip,用于转发

只有通过变量设置域名,resolve的dns解析才会生效

upstream负载均衡

在上面,我们已经了解到了upstream可以设置代理服务器的ip,转发请求,这个代理ip,是可以设置多个的,例如:

代码语言:javascript
复制
upstream 1.cn {
     server 192.168.192.134:80;
     server 127.0.0.1:80;
     server 192.168.192.139:80;
}

server
{
    listen 80;
    server_name x.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/1.cn;
	location / {
     		set $testCn 1.cn;
            proxy_pass http://$testCn;
     }
}

我们同时在3台服务器,新增网站1.cn,并且配置不一样的数据显示,用于区分.

注意: 如果nginx代理本机,请不要访问同一个域名,例如,本机访问1.cn,会造成转发到本机的1.cn->再转发到本机的1.cn,导致出错

所以我在这边的配置项改为了访问主服务器的x.cn,代理到主服务器,以及其他2台副服务器的1.cn中.

仙士可博客
仙士可博客
仙士可博客
仙士可博客
仙士可博客
仙士可博客

这样就实现了nginx负载均衡

upstream权重负载

通过设置 weight,即可设置权重区分负载均衡.例如:

代码语言:javascript
复制
upstream 1.cn {
     server 192.168.192.134:80 weight=2;
     server 127.0.0.1:80 weight=5;
     server 192.168.192.139:80;
}

server
{
    listen 80;
    server_name x.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/x.cn;
	location / {
     		set $testCn 1.cn;
            proxy_pass http://$testCn;
     }
}

设置权重后,nginx在代理时将尽可能平均的前提下通过权重大小进行分配代理服务器.例如a=5,b=3,c=2

请求代理顺序可能为 abacabacaba,在尽量平均请求的前提下增加节点的请求数.

upstream ip hash取模分组

代码语言:javascript
复制
upstream 1.cn {
     ip_hash;
     server 192.168.192.134:80 weight=2;
     server 127.0.0.1:80 weight=5;
     server 192.168.192.139:80;
}

server
{
    listen 80;
    server_name x.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/x.cn;
	location / {
     		set $testCn 1.cn;
            proxy_pass http://$testCn;
     }
}

增加ip_hash配置项后,weight将失效,nginx将通过请求ip进行取模,同一ip的请求将分配到固定的一台服务器上

upstream backup

当其他上游节点全部出现异常时,nginx才会将请求转发到backup:

代码语言:javascript
复制
upstream 1.cn {
     server 192.168.192.134:80;
     server 127.0.0.1:80 backup;
     server 192.168.192.139:80;
}

server
{
    listen 80;
    server_name x.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/x.cn;
	location / {
     		set $testCn 1.cn;
            proxy_pass http://$testCn;
     }
}

本文来自仙士可博客www.php20.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • proxy_pass、upstream与resolver
    • upstream
      • proxy_pass反向代理
        • 反向代理配置项:
      • resolve
      • upstream负载均衡
      • upstream权重负载
      • upstream ip hash取模分组
      • upstream backup
      相关产品与服务
      负载均衡
      负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档