In Nginx, I'm trying to define a variable which allows me to configure a sub-folder for all my location blocks. I did this:
set $folder '/test';
location $folder/ {
[...]
}
location $folder/something {
[...]
}
Unfortunately, this doesn't seem to work. While Nginx doesn't complain about the syntax, it returns a 404 when requesting /test/
. If I write the folder in explicitly, it works. So how can I use variables in location blocks?
A modified python version of @danack's PHP generate script. It generates all files & folders that live inside of
build/
to the parent directory, replacing all{{placeholder}}
matches. You need tocd
intobuild/
before running the script.File structure
build.py
You could do the opposite of what you proposed.
You can't. Nginx doesn't really support variables in config files, and its developers mock everyone who ask for this feature to be added:
You should either write or download a little tool that will allow you to generate config files from placeholder config files.
Update The code below still works, but I've wrapped it all up into a small PHP program/library called Configurator also on Packagist, which allows easy generation of nginx/php-fpm etc config files, from templates and various forms of config data.
e.g. my nginx source config file looks like this:
And then I have a config file with the variables defined:
And then I generate the actual config file using that. It looks like you're a Python guy, so a PHP based example may not help you, but for anyone else who does use PHP:
And then deployConfig.ini looks something like: