Nginx - make proxy_pass work with URI

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block

When you are using regular expression in location, or use if, nginx -t will say this.

# not working versions
location ^~ /api/{
    proxy_pass http://localhost:3000/api/;
}

location / {
    if($host = a.domain.com){
        proxy_pass http://localhost:1234/a/;
    }
}

Workaround: use rewrite

location / {
    if($host = a.domain.com){
        rewrite ^ /a$1;
        proxy_pass http://localhost:1234;
    }
}