nginx and php-fpm: "Primary script unknown" while reading response header from upstream

Solution 1:

I finally solved it!

Turns out that the root location had to match the location of the files on the php-fpm container as that's what was being past through via:

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name

I didn't need to specify that line as it was included in: include fastcgi.conf;.

I'm not sure if this is the most elegant solution:

upstream php {
  server php:9000;
}

server {
  listen 80 default_server;
  server_name _;
  index index.html index.php;
  root /usr/share/nginx/html/public;

  location /api {
    root /usr/share/nginx/html;
    try_files $uri /api/index.php$is_args$args;

    location ~ \.php$ {
      fastcgi_pass php;
      fastcgi_split_path_info ^(.+\.php)(/.*)$;
      include fastcgi.conf;
      internal;
    }
  }
}