Skip to content

Instantly share code, notes, and snippets.

@spro
Last active August 29, 2015 14:13
Show Gist options
  • Save spro/2afede5e48341dfcc67b to your computer and use it in GitHub Desktop.
Save spro/2afede5e48341dfcc67b to your computer and use it in GitHub Desktop.
hipache reimplimented in lua in nginx
location / {
set $proxy_to "";
access_by_lua "
local redis = require 'redis'
local red = redis:new()
red:connect('127.0.0.1', 6379)
local headers = ngx.req.get_headers()
local hosts = red:smembers('frontend:' .. headers['host'])
local chosen = hosts[math.random(#hosts)]
ngx.var.proxy_to = chosen
";
proxy_pass http://$proxy_to;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment