// another day in paradise hacking code and more

Chef'ing Custom Nginx Configs With the Nginx Cookbook

The nginx cookbook has been
super helpful Chef’ing some web apps recently. One thing I struggled to
understand was how to use my own custom conf, like /etc/nginx/nginx.conf, that
is optimized for how I use nginx.

One solution I tried, which is probably a Chef anti-pattern, is to only include
the nginx cookbook on the initial converge:

I knew this was wrong when I wrote it. Chef is all about idempotency.
But, I couldn’t figure out a way to keep the nginx cookbook from reverting my
custom conf during subsequent converges, only to have my template restore my
custom conf a few seconds later.

The Better Way

The OpsCode blog Doing Wrapper Cookbooks Right shows the right way, and really opened my eyes on the power of
Chef’s two phase model (compile, then converge).

123456

include_recipe'nginx'# use our custom nginx.conf, rather than the one that ships in the nginx cookbook# this avoids the nginx and my-app cookbooks from fighting for control of# the same target fileresources('template[nginx.conf]').cookbook'my-app'