How to use a variable in a regular expression in nginx?

There is no way to use variables in location matching.

If you want it, you probably doing something wrong. In this case you could try nesting locations.