mongoose/examples/load_balancer
Deomid Ryabkov bafc30bec6 Change from using #ifdef to #if for features tests
"#if FOO" still works with simple -DFOO, but gives more flexibility.
Specifically, if user expressed no preference (FOO is not defined),
we can apply reasonable defaults (this is the legitimate use of ifdef).

In short, from now on, please use

 #if MG_ENABLE_FOO

instead of

 #ifdef MG_ENABLE_FOO

Since we are all used to #ifdef, this change also adds a precommit check
to police this. Specifically, in *.h and *.c files that are Copyright Cesanta,
"ifdef" and "if defined()" are not allowed to be used with macros that contain
ENABLE or DISABLE, unless the like also contains "ifdef-ok".

Hence, if you are sure you want to use ifdef, use this:

 #ifdef MG_ENABLE_FOO /* ifdef-ok */

PUBLISHED_FROM=9be829448f53cff575d6cae8b9945fb12531c15a
2016-10-13 18:07:25 +00:00
..
Dockerfile Rename Mongoose constants: NS_ -> MG_, NSF_ -> MG_F_ 2015-09-21 15:19:34 +01:00
load_balancer.c Change from using #ifdef to #if for features tests 2016-10-13 18:07:25 +00:00
Makefile Rename Mongoose constants: NS_ -> MG_, NSF_ -> MG_F_ 2015-09-21 15:19:34 +01:00
README.md Merge dev branch code named Fossa as next stable Mongoose 2015-09-08 14:34:30 +02:00
unit_test.sh Merge dev branch code named Fossa as next stable Mongoose 2015-09-08 14:34:30 +02:00

Mongoose-based HTTP load balancer

Configuration

Load balancer is configured with command-line flags.

Global flags

  • -p port TCP port to listen on. Default: 8000.
  • -l log_file path to the log file. Default: none.
  • -s ssl_cert path to SSL certificate. Default: none.

Backend configuration

Main flag is -b uri_prefix host_port it adds a new backend for a given URI prefix. Example: -b /stuff/ 127.0.0.1:8080 will route all requests that start with '/stuff/' to a backend at port 8080 on localhost. There is a special syntax for uri_prefix that allows you to change the URIs that get passed to backends:

  • -b /stuff/=/ 127.0.0.1:8080 for '/stuff/thing' backend will see '/thing'.
  • -b /stuff/=/other/ 127.0.0.1:8080 '/stuff/thing' => '/other/thing'.

Also there are few per-backend flags that can be placed before -b and apply only to the next backend:

  • -r instead of proxying requests load balancer will reply with 302 redirect.
  • -v vhost match not only URI prefix but 'Host:' header as well.

Example

load_balancer -s path/to/cert.pem \
  -v example.com -b /site/=/ 127.0.0.1:8080 \
  -b /static/ 127.0.0.1:8081 \
  -b /static/ 127.0.0.1:8082

In this example requests to 'example.com/site/' will be forwarded to the backend on port 8080 with '/site' prefix stripped off and requests to '/static/' on any virtual host will be balanced in round-robin fashion between backends on ports 8081 and 8082.