>Specifically, if can’t be used within a regular expression based if test, even if you aren’t using variable substitution in the proxy_pass itself.
It seems that you just should not use if()s in nginx.conf for such kind of things, it is rather slow and not intended for request-uri case handling; it is supposed to be used in specific cases like user-agent checks etc.
use regexp locations instead.
to avoid copy-pasting, put some directives to the server{} level, or use the include directive.