Nginx wildcard/regex in location path

Solution 1:

The = modifier in location block is an exact match, without any wildcards, prefix matching or regular expressions. That's why it doesn't work.

On your regex attempt, [a-z] matches a single character between a and z. That's why it doesn't work for you.

You need to have your locations set up like the following. Note the order of location statements. nginx picks the first matching regex condition.

location ~ ^/sitename/[0-9a-z]+/index.php$ {
    fastcgi_pass phpcgi;
}

location ~ \.php$ {
    return 404;
}

I use case sensitive matching here (~ modifier instead of ~*). In the first case, I match the first part of path, then one or more number of alphabetic / number characters and then index.php. You can modify the match range, but remember the + for "one or more" repetitions.

The second one matches any URI ending with .php. You don't need the extra characters in your version because of the way regular expressions work.

Solution 2:

Order is important, from nginx's "location" description:

To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.

It means:

  • First =. ("longest matching prefix" match)
  • Then implicit ones. ("longest matching prefix" match)
  • Then regex. (first match)

You need to adjust the order of regex parts.