Configuration logic

Starting from 1.1 certain logic constructs are available.

The following statements are currently supported:

  • for .. endfor
  • if-dir / if-not-dir
  • if-env / if-not-env
  • if-exists / if-not-exists
  • if-file / if-not-file
  • if-opt / if-not-opt
  • if-reload / if-not-reload – undocumented

Each of these statements exports a context value you can access with thespecial placeholder %(). For example, the “for” statement sets %() tothe current iterated value.

Warning

Recursive logic is not supported and will cause uWSGI to promptly exit.

for

For iterates over space-separated strings. The following three code blocks are equivalent.

  1. [uwsgi]
  2. master = true
  3. ; iterate over a list of ports
  4. for = 3031 3032 3033 3034 3035
  5. socket = 127.0.0.1:%(_)
  6. endfor =
  7. module = helloworld
  1. <uwsgi>
  2. <master/>
  3. <for>3031 3032 3033 3034 3035</for>
  4. <socket>127.0.0.1:%(_)</socket>
  5. <endfor/>
  6. <module>helloworld</module>
  7. </uwsgi>
  1. uwsgi --for="3031 3032 3033 3034 3035" --socket="127.0.0.1:%(_)" --endfor --module helloworld

Note that the for-loop is applied to each line inside the blockseparately, not to the block as a whole. For example, this:

  1. [uwsgi]
  2. for = a b c
  3. socket = /var/run/%(_).socket
  4. http-socket = /var/run/%(_)-http.socket
  5. endfor =

is expanded to:

  1. [uwsgi]
  2. socket = /var/run/a.socket
  3. socket = /var/run/b.socket
  4. socket = /var/run/c.socket
  5. http-socket = /var/run/a-http.socket
  6. http-socket = /var/run/b-http.socket
  7. http-socket = /var/run/c-http.socket

if-env

Check if an environment variable is defined, putting its value in the contextplaceholder.

  1. [uwsgi]
  2. if-env = PATH
  3. print = Your path is %(_)
  4. check-static = /var/www
  5. endif =
  6. socket = :3031

if-exists

Check for the existence of a file or directory. The context placeholder is setto the filename found.

  1. [uwsgi]
  2. http = :9090
  3. ; redirect all requests if a file exists
  4. if-exists = /tmp/maintenance.txt
  5. route = .* redirect:/offline
  6. endif =

Note

The above example uses uWSGI internal routing.

if-file

Check if the given path exists and is a regular file. The context placeholderis set to the filename found.

  1. <uwsgi>
  2. <plugins>python</plugins>
  3. <http-socket>:8080</http-socket>
  4. <if-file>settings.py</if-file>
  5. <module>django.core.handlers.wsgi:WSGIHandler()</module>
  6. <endif/>
  7. </uwsgi>

if-dir

Check if the given path exists and is a directory. The context placeholder isset to the filename found.

  1. uwsgi:
  2. socket: 4040
  3. processes: 2
  4. if-dir: config.ru
  5. rack: %(_)
  6. endif:

if-opt

Check if the given option is set, or has a given value. The contextplaceholder is set to the value of the option reference.

To check if an option was set, pass just the option name to if-opt.

  1. uwsgi:
  2. cheaper: 3
  3. if-opt: cheaper
  4. print: Running in cheaper mode, with initially %(_) processes
  5. endif:

To check if an option was set to a specific value, passoption-name=value to if-opt.

  1. uwsgi:
  2. # Set busyness parameters if it was chosen
  3. if-opt: cheaper-algo=busyness
  4. cheaper-busyness-max: 25
  5. cheaper-busyness-min: 10
  6. endif:

Due to the way uWSGI parses its configs, you can only refer to optionsthat uWSGI has previously seen. In particular, this means:

  • Only options that are set above the if-opt option are taken intoaccount. This includes any options set by previous include (ortype specific includes like ini) options, but does not includeoptions set by previous inherit options).

  • if-opt is processed after expanding magic variables, but beforeexpanding placeholders and other variables. So if you use if-optto compare the value of an option, check against the value as statedin the config file, with only the magic variables filled in.

If you use the context placeholder %(_) inside the if-optblock, you should be ok: any placeholders will later be expanded.

  • If an option is specified multiple times, only the value of the firstone will be seen by if-opt.

  • Only explicitly set values will be seen, not implicit defaults.

See also

How uWSGI parses config files