# Load balancer for Tangle accelerator (using Nginx) ## Introduction The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, and grpc_pass directives. ## Example Configuration * Directives * weight * The default weight is 1 * Weight 2 will be sent twice as much traffic as weight 1 ``` upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; } server { location / { proxy_pass http://backend; } } ``` ## Limitation * Health check * Not active * If an error occurs during communication with a server, the request will be passed to the next server, and so on until all of the functioning servers will be tried. If a successful response could not be obtained from any of the servers, the client will receive the result of the communication with the last server. * Plus version * Active Health Checks ## Load balancer demo site * DEVNET * http://node0.puyuma.org:8000 ## Upstream backend * DEVNET * node2.puyuma.org:8000 * node3.puyuma.org:8000 * node4.puyuma.org:8000 * node5.puyuma.org:8000 * node6.puyuma.org:8000 * node0.puyuma.org:8001 * node10.puyuma.org:8001 * node.deviceproof.org:8000 ## Service file #### Nginx configuration * /etc/nginx/conf.d/load_balancer.conf ``` log_format upstreamlog '[$time_local] $remote_addr - $remote_user - $server_name to: $upstream_addr: $request upstream_response_time $upstream_response_time msec $msec request_time $request_time'; access_log /var/log/nginx/load_balancer.log upstreamlog buffer=32k; ``` * Log format ``` [06/Sep/2018:15:24:40 +0800] 220.134.55.99 - - - _ to: 140.131.178.244:8000, 140.116.82.61:8001: GET / HTTP/1.1 upstream_response_time 3.011, 0.003 msec 1536218680.481 request_time 3.014 [06/Sep/2018:15:24:40 +0800] 220.134.55.99 - - - _ to: 140.116.245.161:8001: GET /favicon.ico HTTP/1.1 upstream_response_time 0.002 msec 1536218680.955 request_time 0.002 [06/Sep/2018:15:24:42 +0800] 220.134.55.99 - - - _ to: 140.116.82.61:8001: GET / HTTP/1.1 upstream_response_time 0.002 msec 1536218682.078 request_time 0.002 [06/Sep/2018:15:24:42 +0800] 220.134.55.99 - - - _ to: 140.116.245.161:8001: GET /favicon.ico HTTP/1.1 upstream_response_time 0.002 msec 1536218682.234 request_time 0.002 [06/Sep/2018:15:25:01 +0800] 220.134.55.99 - - - _ to: 140.116.82.61:8001: GET / HTTP/1.1 upstream_response_time 0.002 msec 1536218701.725 request_time 0.002 [06/Sep/2018:15:25:01 +0800] 220.134.55.99 - - - _ to: 140.116.245.161:8001: GET /favicon.ico HTTP/1.1 upstream_response_time 0.002 msec 1536218701.890 request_time 0.002 [06/Sep/2018:15:25:01 +0800] 220.134.55.99 - - - _ to: 140.116.82.61:8001: GET / HTTP/1.1 upstream_response_time 0.001 msec 1536218701.905 request_time 0.001 [06/Sep/2018:15:25:02 +0800] 220.134.55.99 - - - _ to: 140.116.245.161:8001: GET /favicon.ico HTTP/1.1 upstream_response_time 0.002 msec 1536218702.079 request_time 0.002 [06/Sep/2018:15:25:05 +0800] 220.134.55.99 - - - _ to: 140.131.178.244:8000, 140.116.82.61:8001: GET / HTTP/1.1 upstream_response_time 3.011, 0.003 msec 1536218705.101 request_time 3.014 [06/Sep/2018:15:25:05 +0800] 220.134.55.99 - - - _ to: 140.116.245.161:8001: GET /favicon.ico HTTP/1.1 upstream_response_time 0.002 msec 1536218705.549 request_time 0.002 ``` ## Reference * [Module ngx_http_upstream_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html) * [Nginx Compare versions](https://www.nginx.com/products/nginx/#compare-versions) * [ How To Set Up Nginx Load Balancing ](https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-load-balancing) ## TODO * Install Nginx `ngx_http_log_module` module to record the reverse proxy upstream server serving * [how-to-record-reverse-proxy-upstream-server-serving-request-in-nginx-log](https://stackoverflow.com/questions/18627469/how-to-record-reverse-proxy-upstream-server-serving-request-in-nginx-log) * [Nginx Module ngx_http_log_module](http://nginx.org/en/docs/http/ngx_http_log_module.html) * Auth2 support * https://github.com/jirutka/ngx-oauth