Nginx and PHP-FPM: Fix ‘Primary Script Unknown’ Error

docker-composenginxphp-fpm

I want pass any api/* route to php-fpm. Specifically to index.php as I use Symfony. This is the only route that should use php. Anything else will be loaded from /usr/share/nginx/html/public (just HTML files and CSS).

I've attempted this, but I'm getting an error:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

My nginx config is below:

upstream php {
  server php:9000;
}

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

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

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

I get the following error:

php_1  | [13-Jan-2019 23:22:54] NOTICE: fpm is running, pid 1
php_1  | [13-Jan-2019 23:22:54] NOTICE: ready to handle connections
php_1  | 172.25.0.3 -  13/Jan/2019:23:22:57 +0000 "GET /api/index.php" 404
web_1  | 2019/01/13 23:22:57 [error] 10#10: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,
client: 172.25.0.1, server: impressive.local, request: "GET /api/index.php HTTP/1.1", upstream: "fastcgi://172.25.0.2:9000", host: "127.0.0.1:8080"
web_1  | 172.25.0.1 - - [13/Jan/2019:23:22:57 +0000] "GET /api/index.php HTTP/1.1" 404 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"

I've googled for hours, viewwed multiple other answers on Stack Exchange. I can't seem to figure out what is causing this. I use docker. Below is my docker-compose.yml

version: '3'
services:
  web:
    image: nginx:alpine
    volumes:
      - ./web:/usr/share/nginx/html
      - ./conf/impressive.template:/etc/nginx/conf.d/default.conf
    ports:
      - "8080:80"
    links:
      - php

  php:
    image: php:7.3.1-fpm-alpine
    volumes:
      - ./web:/usr/share/nginx/html

/usr/share/nginx/html is structured like so:

- api
    index.php
- public
    index.html
    something.html
    ...

api is a JSON API, and public is the static site.

I want any requests to api/* to call api/index.php and anything else to go via /usr/share/nginx/html/public.

Best Answer

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;
    }
  }
}
Related Topic