2

I'm working on a legacy API project written in pure PHP and now trying to move it to Symfony 4 and need to make some changes to the NGINX configuration.

I am required to keep both the old and the new endpoints working at the same time, so my plan is to setup NGINX in a way that it tries to serve first the old endpoint and if it doesn't exist (since I plan to remove them as I migrate to Symfony) it redirects to Symfony's front controller.

The current directory structure is this:

Directory structure

So far my NGINX conf is like so:

server {

  root /project/www;
  index index.php;

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php-fpm:9000;
    include fastcgi_params;
    fastcgi_param  HTTPS on;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  location ~ / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

}

It works fine, if I request /api/client_status it uses the index.php under the client_status directory and if I remove that folder it uses Symfony's front controller. The thing is that other applications are using these enpoints sometimes with index.php appended, e.g /api/client_status/index.php, and in these cases I get a 404 instead of being redirected to Symfony's front controller.

I tried rewriting at the top of the server block with rewrite ^/api/(.*)/index.php /api/$1; but it didn't help. Any input or even a suggestion of another approach is very welcome.

William J.
  • 1,574
  • 15
  • 26

1 Answers1

1

The URIs with index.php appended, will be processed by the location ~ \.php$. You need to add a try_files statement to this block to avoid the 404 responses, and instead send the requests to Symfony's front controller.

For example:

location ~ \.php$ {
    try_files $uri /index.php$is_args$args;
    ...
}
Richard Smith
  • 45,711
  • 6
  • 82
  • 81