0

I'm creating PHP backend app using Docker alpine-nginx, I need to redirect all requests starting with /api to run www/index.php file as it's built on MVC framework.

Proxy_pass works great for the rest of the site with NextJS (React, Node)

But Nginx downloads my source code instead passing it to FPM. And no other question on that topic helped me,nor moving the location block outside another location.

Do you see anything? If I remove nested location, It runs PHP-FPM, but get error Primary script unknown" while reading response header from upstream, trying to understand, what is wrong...


EDIT: Complete conf is here

upstream next_app {
  # NextJS running app port
  server nextapp:3000;
}

upstream php_fpm {
  # PHP FPM server URI and port
  server phpapp:9000;
}

server {
  listen 80 default_server;

  server_name _;

  server_tokens off;

  error_log /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;

  location /api {
      root /var/www;
      try_files /www/index.php =404;

      include fastcgi_params;
      fastcgi_pass php_fpm;
  }

  # proxy pass for NodeJS app
  proxy_http_version 1.1;

  proxy_set_header Host                $http_host;
  proxy_set_header X-Real-IP           $remote_addr;
  proxy_set_header X-Forwarded-For     $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto   $scheme;
  proxy_set_header Host                $host;
  proxy_cache_bypass $http_upgrade;

  location / {
    proxy_pass http://next_app;
  }
}

Now it's hitting the PHP-FPM I suppose, but telling me File not found and FPM is logging nothing. Nginx tells me FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

sjiamnocna
  • 21
  • 3

2 Answers2

0

Remove the nested location.

Try:

location /api {
    root /var/www;
    try_files /www/index.php =404;

    include fastcgi_params;
    fastcgi_pass php_fpm;
}
Richard Smith
  • 11,859
  • 2
  • 18
  • 26
  • Well, I already tried it, so again - got `FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream`. At least it's running from FPM now. The files are present, I checked it via shell. – sjiamnocna Jul 22 '22 at 15:57
0

OK, thanks to first answer, I experimented on that a little. Looked on notes on Nginx FASTCGI notex, the 4th advice to set SCRIPT_FILENAME as in alpine it seems that it's not included

Now it works, somehow with this code;

location /api {
  root /var/www;
  try_files /www/index.php =404;

  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  fastcgi_pass php_fpm;
}
sjiamnocna
  • 21
  • 3