🔁 How to Build a Lightweight Round-Robin HTTP Redirector Using OpenResty
Sometimes, you only need simple HTTP redirection to multiple backend URLs in a round-robin fashion — without load balancers or proxying content.
Here’s how you can achieve it using OpenResty (NGINX + Lua).
🛠 Prerequisites
-
Ubuntu system (x86_64 / amd64)
-
Root or sudo privileges
1️⃣ Install OpenResty
2️⃣ Configure NGINX for Round-Robin Redirects
Edit the main NGINX configuration file:
Add the following inside the http { ... }
block:
How it works:
-
lua_shared_dict rr_counter 1m;
creates a small shared memory segment to track the current URL index. -
Each request increments the counter modulo the number of URLs.
-
NGINX issues a 302 redirect to the next URL in sequence.
-
/health
endpoint can be used for load balancer or CloudFront health checks.
3️⃣ Reload OpenResty
4️⃣ Test the Redirect
Expected output:
Requests cycle through the URLs in order — true round-robin redirection.
✅ Advantages
-
Minimal server load (no content proxying)
-
True round-robin distribution
-
Works perfectly behind ELB or CloudFront
-
Easily extendable to multiple domains or URL lists
No comments:
Post a Comment