Dynamic Nginx
Two days ago I encountered a big problem. I needed to build multiple nginx images for different application stacks, that’s not cool at all. Yes I could use Consul and registrator to actually have proper service discovery within each stack cluster but I didn’t wanted to have this overhead.
Wouldn’t it be great if could just give Nginx some env variables in docker-compose so that he knows what to balance from the start? Well I though that was worth investigating.
First I tried search for something already built, At some point I found a great implementation of haproxy done by tumtum but it wouldn’t really satisfy my need, I have multiple paths/ports in some of my services and I couldn’t just serve everything from port 80.
So then I gave it a go and written my own container, that just does what I needed. Nothing to fancy just a bash script that interprets specific env variables and creates locations and upstreams in nginx.
So now I can just have a few variables in compose like so:
1 2 |
|
and this creates the fallowing in nginx.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
If you want to try it out checkout the image on dockerhub:
If you want to add functionality to this, feel free to fork svidrascu/dynginx
Now I know that this is not really fully configurable, I could have even more power on the location directive but for now, it’s what I need :)