My way of organizing Nginx config


Some year or so ago I started extracing a lot of directives into snippets and tried to give the snippets readable paths.

So instead of

location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
include snippets/php/fastcgi-php.conf;

I now have

include snippets/php/7.4;

which I find much easier to comprehend quickly and obviously less cluttering.

I understand that this might not be necessarry for many use cases, but I usually manage servers with multiple (sometimes a dozen) of generic PHP+MySQL and similar sites. I have so far enjoyed that the vhost files are concise and the only thorough descriptions are on whatever is non-standard for that site.

Do you do something similar or better? Or maybe you find my approach to be dumb?

I put a sketch up on GitHub to simplify reusing and share it with my colleagues. I'd appreciate any comments and suggestions if you have the time to take a look:

3 thoughts on “My way of organizing Nginx config”

  1. I manage Nginx configs this way as well. Anything that will be used by multiple sites gets put into a seperate file, and included.

  2. Is there a way to pass variables to include for situations where you have boilerplate code to include that, for instance, has a different key for each site?


Leave a Comment