Is it possible to extend a 504 timeout in nginx on a per location basis

First, take a look at nested locations. The reason why your second location block isn't taken into account is because when nginx matches a location, it stops. So, http://ubuntu-vm.test-api/someurlpath, if theres an index.php in the corresponding folder, only matches the location ~ \.php$ !

I've stumbled upon this interesting blog post

To sum this up, you need to:

  1. Increase the max_execution_time configuration variable in your php.ini.
  2. Increase the request_terminate_timeout configuration variable of php-fpm.
  3. Set the fastcgi_read_timeout in the location you want, in the nginx configuration file.

The trouble is you can't tell php-fpm to use a different configuration file only for that one location.

However, you can set a php.ini configuration variable in your nginx configuration like so:

fastcgi_param PHP_VALUE "max_execution_time=1000";