# Lua back-end load balancing

**URL:** <https://www.ruby-forum.com/t/lua-back-end-load-balancing/208094>\
**Category:** NGINX\
**Created:** [June 10, 2011, 1:41am UTC](https://www.ruby-forum.com/t/lua-back-end-load-balancing/208094 "2011-06-10T01:41:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 10, 2011, 1:41am UTC](https://www.ruby-forum.com/t/lua-back-end-load-balancing/208094/1 "2011-06-10T01:41:22Z")

</div>

Hi  
I’m using nginx-lua to get some info from a back end “auth” server for  
each request  
The back end server is a simple http server written by myself - i.e. it  
is nowhere near as fast or as optimised as nginx and can therefore be  
considered a bottleneck  
I could run multiple backend server instances to give it a better chance  
of keeping up (i.e. one per cpu core) - but then I’d need to load  
balance (only round-robin style) to the multiple backends in my lua  
code.  
Is there something I can do in lua code to accomplish this?

Many thanks as always

---

<div class="post-metadata">

**Author:** ![rkoskela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rkoskela](https://www.ruby-forum.com/u/rkoskela)\
**Post date:** [June 10, 2011, 6:31am UTC](https://www.ruby-forum.com/t/lua-back-end-load-balancing/208094/2 "2011-06-10T06:31:26Z")

</div>

On Fri, Jun 10, 2011 at 7:40 AM, Richard K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Im using nginx-lua to get some info from a back end auth server for each  
> request

Sounds cool 🙂

> The back end server is a simple http server written by myself i.e. it is  
> nowhere near as fast or as optimised as nginx and can therefore be  
> considered a bottleneck
> 
> I could run multiple backend server instances to give it a better chance of  
> keeping up (i.e. one per cpu core) but then Id need to load balance (only  
> round-robin style) to the multiple backends in my lua code.

Why not use the nginx upstream block directly? For example:

```
upstream backend {
     server 10.32.56.4;
     server 10.32.56.3;
}

location = /auth {
     proxy_pass http://backend;
}

```

No Lua is required here unless you need to custimize the load  
balancing algorithm.

> Is there something I can do in lua code to accomplish this?

If you insist in using Lua, just a quick note on this: the standard  
ngx\_proxy module supports dynamic resolvers by interpolating nginx  
variables into the proxy\_pass directive. I think you can use Lua to  
compute the value of your nginx variables, which points to a custom  
backend server.

Regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![rkoskela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rkoskela](https://www.ruby-forum.com/u/rkoskela)\
**Post date:** [June 10, 2011, 1:04pm UTC](https://www.ruby-forum.com/t/lua-back-end-load-balancing/208094/3 "2011-06-10T13:04:28Z")

</div>

Ofcourse!  
Many thanks
