Chinaunix首页 | 论坛 | 博客
  • 博客访问: 419733
  • 博文数量: 124
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 872
  • 用 户 组: 普通用户
  • 注册时间: 2018-03-29 14:38
个人简介

默默的一块石头

文章分类

全部博文(124)

文章存档

2022年(26)

2021年(10)

2020年(28)

2019年(60)

我的朋友

分类: LINUX

2021-12-22 15:31:55

测试环境:
1.ubuntu 18.04
2.nginx-1.15.6

实验:
1.基于nginx-1.15.6添加debug code
 static int lucount = 0;

ngx_http_upstream_round_robin.c
static ngx_http_upstream_rr_peer_t *
ngx_http_upstream_get_peer(ngx_peer_connection_t *pc,ngx_http_upstream_rr_peer_data_t *rrp)
{
    time_t                        now;
    uintptr_t                     m;
    ngx_int_t                     total;
    ngx_uint_t                    i, n, p;
    ngx_http_upstream_rr_peer_t  *peer, *best;

    now = ngx_time();

    best = NULL;
    total = 0;

#if (NGX_SUPPRESS_WARN)
    p = 0;
#endif
    //ngx_http_upstream_init_round_robin(){ us->peer.data = peers; }
    //ngx_http_upstream_init_round_robin_peer() { rrp->peers = us->peer.data; }
    for (peer = rrp->peers->peer, i = 0;
         peer;
         peer = peer->next, i++)
    {
        n = i / (8 * sizeof(uintptr_t));
        m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));

        if (rrp->tried[n] & m) {
            continue;
        }

        if (peer->down) {
            continue;
        }

        if (peer->max_fails
            && peer->fails >= peer->max_fails
            && now - peer->checked <= peer->fail_timeout)
        {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, pc->log, 0,"ngx_http_upstream_get_peer() 544");
            continue;
        }
        if (peer->max_conns && peer->conns >= peer->max_conns) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, pc->log, 0,"ngx_http_upstream_get_peer() 549");
            continue;
        }
    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,"ngx_http_upstream_get_peer() 552 peer->current_weight = %d peer->name = %V",peer->current_weight,&peer->name);
        peer->current_weight += peer->effective_weight;
        total += peer->effective_weight;
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, pc->log, 0,"ngx_http_upstream_get_peer() 555 peer->current_weight = %d peer->effective_weight = %d peer->name = %V",peer->current_weight,peer->effective_weight,&peer->name);
        if (peer->effective_weight < peer->weight) {
            peer->effective_weight++;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,"ngx_http_upstream_get_peer() 558 peer->weight = %d peer->effective_weight = %d",peer->weight,peer->effective_weight);
        }

        if (best == NULL || peer->current_weight > best->current_weight) {
            best = peer;
            p = i;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,"ngx_http_upstream_get_peer() 563 i = %d",i);
        }
    }

    if (best == NULL) {
        return NULL;
    }

    rrp->current = best;

    n = p / (8 * sizeof(uintptr_t));
    m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));

    rrp->tried[n] |= m;
    best->current_weight -= total;
rrp->peers->count = rrp->peers->count + 1;
lucount = lucount + 1;
    ngx_log_debug6(NGX_LOG_DEBUG_HTTP, pc->log, 0,"ngx_http_upstream_get_peer() 578 rrp->tried[n] = %d best->name = %V best->current_weight = %d rrp->peers->count = %d lucount = %d getpid() = %d",rrp->tried[n],&best->name,best->current_weight,rrp->peers->count,lucount,getpid());
    if (now - best->checked > best->fail_timeout) {
        best->checked = now;
    }

    return best;
}

2.开启4个nginx服务,一个作为server三个作为local
(1)server nginx.conf基于模板更改如下:
#user  nobody;
worker_processes     4;
daemon off;

error_log  /home/chro/nginx/logs/error.log;
error_log  /home/chro/nginx/logs/error.log  notice;
error_log  /home/chro/nginx/logs/error.log  info;
error_log  /home/chro/nginx/logs/error.log  debug;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    upstream linuxidc { 
        server 0.0.0.0:7080 weight=1; 
       server 0.0.0.0:8080 weight=2;
        server 0.0.0.0:9080 weight=3; 
    }
    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       6080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /res {
         proxy_pass
        }

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
(1)local nginx.conf基于模板更改如下:
#user  nobody;
worker_processes     4;
daemon off;

error_log  /home/chro/nginx/logs/error1.log;
error_log  /home/chro/nginx/logs/error1.log  notice;
error_log  /home/chro/nginx/logs/error1.log  info;
error_log  /home/chro/nginx/logs/error1.log  debug;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    #gzip  on;

    server {
       #参照serverupstream的port设定,更改listen port 8080,9080再添加两份nginx.conf
        listen       7080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /res {
            alias /home/chro/linux;
          autoindex on;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
3.在浏览器中输入localhost:6080/res n+个窗口,得到debug信息
(1)2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:7080
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 555 peer->current_weight = 1 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:8080
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 555 peer->current_weight = 2 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 563 i = 1
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:9080
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 555 peer->current_weight = 3 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 563 i = 2
2021/12/22 15:01:56 [debug] 4752#0: *1 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 4 best->name = 0.0.0.0:9080 best->current_weight = -3 rrp->peers->count = 1 lucount = 1 getpid() = 4752

(2)2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:7080
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 555 peer->current_weight = 1 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:8080
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 555 peer->current_weight = 2 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 563 i = 1
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:9080
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 555 peer->current_weight = 3 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 563 i = 2
2021/12/22 15:01:59 [debug] 4753#0: *2 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 4 best->name = 0.0.0.0:9080 best->current_weight = -3 rrp->peers->count = 1 lucount = 1 getpid() = 4753

(3)2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:7080
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 555 peer->current_weight = 1 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:8080
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 555 peer->current_weight = 2 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 563 i = 1
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:9080
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 555 peer->current_weight = 3 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 563 i = 2
2021/12/22 15:02:01 [debug] 4754#0: *3 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 4 best->name = 0.0.0.0:9080 best->current_weight = -3 rrp->peers->count = 1 lucount = 1 getpid() = 4754

(4)2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 552 peer->current_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 555 peer->current_weight = 2 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 552 peer->current_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 555 peer->current_weight = 4 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 563 i = 1
2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 552 peer->current_weight = -3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 555 peer->current_weight = 0 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:11 [debug] 4754#0: *9 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 2 best->name = 0.0.0.0:8080 best->current_weight = -2 rrp->peers->count = 2 lucount = 2 getpid() = 4754

(5)2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:7080
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 555 peer->current_weight = 1 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:8080
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 555 peer->current_weight = 2 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 563 i = 1
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:9080
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 555 peer->current_weight = 3 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 563 i = 2
2021/12/22 15:02:02 [debug] 4755#0: *4 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 4 best->name = 0.0.0.0:9080 best->current_weight = -3 rrp->peers->count = 1 lucount = 1 getpid() = 4755

(6)2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 552 peer->current_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 555 peer->current_weight = 2 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 552 peer->current_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 555 peer->current_weight = 4 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 563 i = 1
2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 552 peer->current_weight = -3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 555 peer->current_weight = 0 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:12 [debug] 4755#0: *12 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 2 best->name = 0.0.0.0:8080 best->current_weight = -2 rrp->peers->count = 2 lucount = 2 getpid() = 4755

(7)2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 552 peer->current_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 555 peer->current_weight = 2 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 552 peer->current_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 555 peer->current_weight = 4 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 563 i = 1
2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 552 peer->current_weight = -3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 555 peer->current_weight = 0 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:06 [debug] 4752#0: *5 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 2 best->name = 0.0.0.0:8080 best->current_weight = -2 rrp->peers->count = 2 lucount = 2 getpid() = 4752

(8)2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 552 peer->current_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 555 peer->current_weight = 2 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 552 peer->current_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 555 peer->current_weight = 4 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 563 i = 1
2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 552 peer->current_weight = -3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 555 peer->current_weight = 0 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:09 [debug] 4753#0: *7 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 2 best->name = 0.0.0.0:8080 best->current_weight = -2 rrp->peers->count = 2 lucount = 2 getpid() = 4753

(9)2021/12/22 15:02:19 [debug] 4753#0: *17 ngx_http_upstream_get_peer() 552 peer->current_weight = 2 peer->name = 0.0.0.0:7080
2021/12/22 15:02:19 [debug] 4753#0: *17 ngx_http_upstream_get_peer() 555 peer->current_weight = 3 peer->effective_weight = 1 peer->name = 0.0.0.0:7080
2021/12/22 15:02:19 [debug] 4753#0: *17 ngx_http_upstream_get_peer() 563 i = 0
2021/12/22 15:02:19 [debug] 4753#0: *17 ngx_http_upstream_get_peer() 552 peer->current_weight = -2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:19 [debug] 4753#0: *17 ngx_http_upstream_get_peer() 555 peer->current_weight = 0 peer->effective_weight = 2 peer->name = 0.0.0.0:8080
2021/12/22 15:02:19 [debug] 4753#0: *17 ngx_http_upstream_get_peer() 552 peer->current_weight = 0 peer->name = 0.0.0.0:9080
2021/12/22 15:02:19 [debug] 4753#0: *17 ngx_http_upstream_get_peer() 555 peer->current_weight = 3 peer->effective_weight = 3 peer->name = 0.0.0.0:9080
2021/12/22 15:02:19 [debug] 4753#0: *17 ngx_http_upstream_get_peer() 578 rrp->tried[n] = 1 best->name = 0.0.0.0:7080 best->current_weight = -3 rrp->peers->count = 3 lucount = 3 getpid() = 4753

参考文档: https:// digest/ understandingnginx/ 202586
阅读(626) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~