Nginx chooses a location by a fixed order, not by position in the file. Getting the modifiers wrong is the most common reason a rule silently does not apply.
An exact = match wins first. Then the longest prefix is remembered; if it uses ^~ the search stops. Otherwise regex locations (~ and ~*) are tried in file order, and the first match wins over the prefix.
Paste your pattern and sample URIs into the tester to see exactly what matches, then copy the expression into your config.
Open the Regex tester generator
~ is case-sensitive and ~* is case-insensitive.
Yes. An unescaped dot matches any character, so \.php$ is correct and .php$ is not.