reduced functionality switch - case in nginx: map module

There's no switch-case in nginx, though map is available for similar but reduced functionality cases.

Update (2012-12-20 13:17)

There's a WordPress Plugin, called Nginx ( nginx-helper )1 which can create the required map for WordPress multisite.

I was searching for a switch-case possibility in nginx without success. Although a module named "map" can do something similar but only for setting a variable.

Like the following:

map $host $dirnum {
    default        0;
    domain1.com    1;
    domain2.com    2;
}

This has to be placed outside the server part but inside http.

Afterwards the variable becomes useable, in, for example rewrite rules:

# if domain is mapped
if ($dirnum != 0 ) {
    rewrite ^/files/(.*)$ /wp-content/blogs.dir/$dirnum/files/$1 last;
}
# otherwise fall back to "normal" rule
if ($dirnum = 0 ) {
    rewrite ^(.*)/files/(.*)$ /wp-includes/ms-files.php?file=$2 last;
}

(Oh, by the way: this entry was written by Peter Molnar, and originally posted on petermolnar dot net.)