zoukankan      html  css  js  c++  java
  • Nginx负载均衡配置实例详解

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在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
     
    1. upstream www.threegroup.space {  
    2.       server  123.56.255.53:8080;  
    3.       server  101.200.159.138:8080;  
    4.     }  
    5.     server{  
    6.         listen 80;  
    7.         server_name www.threegroup.space;  
    8.         location / {  
    9.                 proxy_pass         http://www.threegroup.space;  
    10.                 proxy_set_header   Host             $host;  
    11.                 proxy_set_header   X-Real-IP        $remote_addr;  
    12.                 proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
    13.         }  
    14. }  

    如截图所示


    从截图上看到

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

    和 123.56.255.53:8080 的IP上

    最后 保存重启nginx

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

    ② 工作服务器配置方法

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

    [php] view plain copy
     
    1. server{  
    2.         listen 8080;  
    3.         server_name www.threegroup.space;  
    4.         index index.html;  
    5.         root /data0/htdocs/www;  
    6. }  

    如截图所示


    保存重启nginx

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

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

  • 相关阅读:
    HDU5418.Victor and World(状压DP)
    POJ2686 Traveling by Stagecoach(状压DP)
    POJ3254Corn Fields(状压DP)
    HDU5407.CRB and Candies(数论)
    CodeForces 352D. Jeff and Furik
    CodeForces 352C. Jeff and Rounding(贪心)
    LightOj 1282 Leading and Trailing
    Ural 1057. Amount of Degrees(数位DP)
    HDU 2089 不要62 (数位DP)
    HDU5366 The mook jong (DP)
  • 原文地址:https://www.cnblogs.com/pcyy/p/8711665.html
Copyright © 2011-2022 走看看