Skip to content

ZigzagAK/ngx_http_upsync_upstream

Repository files navigation

Name

ngx_http_upsync_upstream - Upstream synced by http request.

Quick Start

Nginx config

http {
  healthcheck_buffer_size 128k;

  upstream app1 {
    zone shm_app1 128k;

    upsync localhost:8888/registry/nodes?service=app1;
    upsync_header Accept text/plain;

    upsync_interval 10s;
    upsync_timeout 10s;

    upsync_defaults max_conns=10 max_fails=1 fail_timeout=30s;
    upsync_file app1.peers;

    dns_update 60s;
    dns_add_down on;

    check passive type=http rise=2 fall=2 timeout=5000 interval=10;
    check_request_uri GET /health;
    check_response_codes 200;
    check_response_body alive;
  }

  upstream app2 {
    zone shm_app2 128k;

    upsync localhost:8888/registry/nodes?service=app2;
    # Admin:1111
    upsync_header Authorization "Basic QWRtaW46MTExMQ==";
    upsync_header Accept text/plain;

    upsync_interval 10s;
    upsync_timeout 10s;

    upsync_defaults max_conns=10 max_fails=1 fail_timeout=30s;
    upsync_file app2.peers;

    dns_update 60s;
    dns_add_down on;

    check passive type=http rise=2 fall=2 timeout=5000 interval=10;
    check_request_uri GET /health;
    check_response_codes 200;
    check_response_body alive;
  }

  upstream app3 {
    zone shm_app3 128k;

    upsync localhost:8888/registry/app3;
    # Admin:1111
    upsync_header Accept text/plain;

    upsync_interval 10s;
    upsync_timeout 10s;

    upsync_defaults max_conns=10 max_fails=1 fail_timeout=30s;
    upsync_file app3.peers;

    dns_update 60s;
    dns_add_down on;

    check passive type=http rise=2 fall=2 timeout=5000 interval=10;
    check_request_uri GET /health;
    check_response_codes 200;
    check_response_body alive;
  }

  server {
    listen 6000;

    location /dynamic {
      dynamic_upstream;
    }
  }

  server {
    # app1
    listen 8001;
    listen 8002;
    listen 8003;

    #app2
    listen 9001;
    listen 9002;
    listen 9003;

    location = /health {
      return 200 'alive';
    }

    location / {
      access_log off;
      return 200 'hello';
    }
  }

  server {
    listen 10000;

    access_log off;

    location /app1 {
      proxy_pass http://app1;
    }

    location /app2 {
      proxy_pass http://app2;
    }
  }

  server {
    listen 8888;

    location = /registry/nodes {
      if ($arg_service = app1) {
        echo "localhost:8001 weight=1 max_conns=10 max_fails=2 fail_timeout=10s";
        echo "localhost:8002 weight=2 max_conns=20 max_fails=2 fail_timeout=5s";
        echo "localhost:8003 max_conns=50 max_fails=2 fail_timeout=10s backup";
      }
      if ($arg_service = app2) {
        echo "localhost:9001";
        echo "localhost:9002";
        echo "localhost:9002 backup";
      }
    }

    location = /registry/app3 {
      alias conf/app3.txt;
    }

    location /dynamic {
      dynamic_upstream;
    }

    location /healthcheck/get {
      healthcheck_get;
    }

    location /healthcheck/status {
      healthcheck_status;
    }
  }
}