Both location blocks provided works fine here.
Note though that using numbered captures is error prone, because numbered captures are used from the last regular expression executed. For example, adding any rewrite
directive to the first location block will break things, as the rewrite
directive will use regular expressions, overwriting the $1 .. $9
variables. The fact that the second location block works for you when the first one doesn't likely indicate that this is exactly what happens in your case.
To further confirm what exactly breaks in your case, please provide full (yet minimal) nginx configuration which is not working. Output of nginx -T
would be optimal.