Advanced load balancer using Cloudflare Workers with weighted, backup, and disabled origin support.
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
This project provides a robust, production-ready Cloudflare Worker that acts as a high-performance HTTP load balancer. It allows you to distribute traffic across multiple origin servers based on configurable weights, with built-in support for failover to backup servers and maintenance modes.
Key Features:
- Weighted Random Selection: Distribute traffic proportionally across primary origins.
- Automatic Failover: Seamlessly routes traffic to backup origins if primary origins are unavailable or return errors.
- Dynamic Configuration: Configure origins, weights, timeouts, and headers via environment variables.
- Health Monitoring: Built-in
/healthand/_lb/statsendpoints for real-time status updates. - Header & Cookie Handling: Accurate forwarding of request/response headers and
Set-Cookiesupport. - Request Tracking: Optional unique request IDs for end-to-end troubleshooting.
- Security-First: Sanitizes hop-by-hop headers and prevents origin URL leakage through Location header rewriting.
To get your load balancer up and running, follow these steps.
You need the following installed:
- Node.js and npm
npm install npm@latest -g
- Wrangler CLI
npm install -g wrangler
- Clone the repo
git clone https://github.com/LoveDoLove/cloudflare-load-balancer.git
- Install NPM packages
npm install
- Update your origin configuration in
wrangler.jsonc(see Usage). - Deploy to Cloudflare
npm run deploy
The load balancer is primarily configured through the ORIGINS_CONFIG variable in wrangler.jsonc. This allows you to update your infrastructure without redeploying code.
An origin object can have the following properties:
| Property | Type | Default | Description |
|---|---|---|---|
url |
string |
Required | The destination origin URL (must include protocol). |
weight |
number |
1 |
Relative traffic weight (higher = more traffic). |
backup |
boolean |
false |
If true, only used if all primary origins fail. |
enabled |
boolean |
true |
Quickly enable/disable an origin. |
timeout |
number |
10000 |
Request timeout in milliseconds. |
headers |
object |
{} |
Custom headers to inject for this specific origin. |
"vars": {
"ORIGINS_CONFIG": "[{\"url\":\"https://primary-1.example.com\",\"weight\":3},{\"url\":\"https://primary-2.example.com\",\"weight\":1},{\"url\":\"https://backup.example.com\",\"backup\":true}]"
}- Health Check:
GET /health- Returns 200 OK if the system is functional. - System Stats:
GET /_lb/stats- Returns detailed information about current configuration and origin pools.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the Apache License 2.0. See LICENSE for more information.
Project Link: https://github.com/LoveDoLove/cloudflare-load-balancer