• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

nginx在做负载均衡时配置

武飞扬头像
PHP中文网
帮助20

nginx做负载均衡是在反向代理的基础上做的

代码如下:

## Basic reverse proxy server ##  
## Apache backend for www.百度.com ##  
upstream henushang  {  
        # 不过最好换成你们的服务器测试,因为我测试的时候使用jd和百度的都没有连接成功,         # 换成自己的服务器就行了,估计是那里有限制,如果哪位知道,请指教  
        server www.jd.com weight=1; # 或者ip:port这样形式也是可以的  
    server www.百度.com weight=9; # 或者ip:port这样形式也是可以的  
}  
  
## Start www.百度.com ##  
server {  
    listen 80;  
    server_name  www.henushang.cn;#监听的域名  
  
    access_log  logs/henushang.access.log;  
    error_log  logs/henushang.error.log;  
    root   html;  
    index  index.html index.htm index.php;  
  
    ## send request back to apache ##  
    location / {  
        proxy_pass  http://henushang;#与上面的upstream名字相对应  
  
        #Proxy Settings  
        proxy_redirect     off;  
        proxy_set_header   Host             $host;  
        proxy_set_header   X-Real-IP        $remote_addr;  
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;  
        proxy_max_temp_file_size 0;  
        proxy_connect_timeout      90;  
        proxy_send_timeout         90;  
        proxy_read_timeout         90;  
        proxy_buffer_size          4k;  
        proxy_buffers              4 32k;  
        proxy_busy_buffers_size    64k;  
        proxy_temp_file_write_size 64k;  
   }  
}

nginx做负载均衡有如下几种方式:

1、RR(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 例如:

upstream tomcats {     
               server 10.1.1.107:88  max_fails=3 fail_timeout=3s weight=9;   
               server 10.1.1.132:80  max_fails=3 fail_timeout=3s weight=9;

2、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 例如:

upstream tomcats {   
           ip_hash;    
           server 10.1.1.107:88;    
           server 10.1.1.132:80;    
}

3、fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4、url_hash(第三方) 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tangbheaf
系列文章
更多 icon
同类精品
更多 icon
继续加载