How uWSGI parses config files

Until uWSGI 1.1 the parsing order has not been ‘stable’ or ‘reliable’.

Starting from uWSGI 1.1 (thanks to its new options subsystem) we have a general rule: top-bottom and expand asap.

Top-bottom means options are internally ordered as they are parsed, while “expand asap” means to inject the options of a requested config file, interrupting the currently parsed one:

Note that the inherit option behaves differently from the other include options: It is expanded after variable expansion, so any environment variables, external files and placeholders are not expanded. Magic variables (e.g. %n) are expanded normally.

file1.ini (the one requested from the command line)

  1. [uwsgi]
  2. socket = :3031
  3. ini = file2.ini
  4. socket = :3032
  5. chdir = /var/www

file2.ini

  1. [uwsgi]
  2. master = true
  3. memory-report = true
  4. processes = 4

internally will be assembled in:

  1. [uwsgi]
  2. socket = :3031
  3. ini = file2.ini
  4. master = true
  5. memory-report = true
  6. processes = 4
  7. socket = :3032
  8. chdir = /var/www

A more complex example:

file1.ini (the one requested from the command line)

  1. [uwsgi]
  2. socket = :3031
  3. ini = file2.ini
  4. socket = :3032
  5. chdir = /var/www

file2.ini

  1. [uwsgi]
  2. master = true
  3. xml = file3.xml
  4. memory-report = true
  5. processes = 4

file3.xml

  1. <uwsgi>
  2. <plugins>router_uwsgi</plugins>
  3. <route>^/foo uwsgi:127.0.0.1:4040,0,0</route>
  4. </uwsgi>

will result in:

  1. [uwsgi]
  2. socket = :3031
  3. ini = file2.ini
  4. master = true
  5. xml = file3.xml
  6. plugins = router_uwsgi
  7. route = ^/foo uwsgi:127.0.0.1:4040,0,0
  8. memory-report = true
  9. processes = 4
  10. socket = :3032
  11. chdir = /var/www

Expanding variables/placeholders

After the internal config tree is assembled, variables and placeholder substitution will be applied.

The first step is substituting all of the $(VALUE) occurrences with the value of the environment variable VALUE.

  1. [uwsgi]
  2. foobar = $(PATH)

foobar value will be the content of shell’s PATH variable

The second step will expand text files embraced in @(FILENAME)

  1. [uwsgi]
  2. nodename = @(/etc/hostname)

nodename value will be the content of /etc/hostname

The last step is placeholder substitution. A placeholder is a reference to another option:

  1. [uwsgi]
  2. socket = :3031
  3. foobar = %(socket)

the content of foobar will be mapped to the content of socket.

A note on magic variables

Config files, support another form of variables, called ‘magic’ variables. As they refer to the config file itself, they will be parsed asap:

  1. [uwsgi]
  2. my_config_file = %p

The content of my_config_file will be set to %p value (the current file’s absolute path) as soon as it is parsed. That means %p (or whatever magic vars you need) will be always be consistent in the currently parsing config file.