Nginx负载均衡配置实例详解【原】

it2022-05-25  67

负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法。

 

测试环境

测试域名  :www.threegroup.space

A服务器IP :123.56.255.173 (主)

B服务器IP :101.200.159.138

C服务器IP :123.56.255.53

 

部署思路A服务器做为主服务器,域名直接解析到A服务器(123.56.255.173)上,由A服务器负载均衡到B服务器(101.200.159.138)与C服务器(123.56.255.53)上。

 

开始配置Nginx负载均衡

①        打开nginx.conf,文件位置在nginx安装目录的conf目录下。

在http段加入以下代码 :

 

[php]  view plain  copy    print? upstream www.threegroup.space {        server  123.56.255.53:8080;        server  101.200.159.138:8080;      }      server{          listen 80;          server_name www.threegroup.space;          location / {                  proxy_pass         http://www.threegroup.space;                  proxy_set_header   Host             $host;                  proxy_set_header   X-Real-IP        $remote_addr;                  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;          }  }  

 

如截图所示

从截图上看到

已成功将www.threegroup.space解析到101.200.159.138:8080  的IP

和 123.56.255.53:8080 的IP上

最后 保存重启nginx

[php]  view plain  copy    print? 拓展知识  通过上面的配置可以发现上面配置的负载均衡是按照1:1的方式来回切换,其实你也可以通过配置文件你可以站点的权重:  upstream  site {     server   192.168.3.82:8040 weight=2;    server   192.168.3.82:8041 weight=1;  }       weight即为对应网站的权重。  

 

② 工作服务器配置方法

我们要在 B、C服务器nginx.conf设置如下打开nginx.confi,在http段加入以下代码

 

[php]  view plain  copy    print? server{          listen 8080;          server_name www.threegroup.space;          index index.html;          root /data0/htdocs/www;  }  

 

如截图所示

 

保存重启nginx

③ 测试当访问www.threegroup.space的时候,为了区分是转向哪台服务器处理我分别在B、C服务器下写一个不同内容的index.html文件,以作区分。

打开浏览器访问www.threegroup.space结果,刷新会发现所有的请求均分别被主服务器(A) 分配到 B服务器(101.200.159.138)与C服务器(123.56.255.53)上,实现了负载均衡效果。

 

推荐学习网站

http://blog.csdn.net/libinemail/article/details/51074866

http://www.php100.com/html/program/nginx/2013/0905/5525.html

http://www.cnblogs.com/liping13599168/archive/2011/04/15/2017369.html

 

转载于:https://www.cnblogs.com/quanzhiguo/p/7366218.html

相关资源:数据结构—成绩单生成器

最新回复(0)