Slim 4 on NGINX with a specific location directive

I have Slim v4 framework installed with nginx/1.22.0 and php-fpm-8.1.9 on RHEL8. Here is the / location block of current nginx.conf that works fine.

  location / {
     root /path/to/html/slim;
     try_files $uri /index.php$is_args$args;
  }

Instead of using location /, I’d like to use a different location for slim, so that / can be used to host another website. I tried below configuration, but slim gives me 404 Not Found with Slim\Exception\HttpNotFoundException. Please help me to figure out a way. Thank you!

  root /path/to/html;

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

  location / {
    try_files $uri =404;
  }

You may take a look into the documentation, see Nginx configuration.

If you run your Slim app not directly under the document-root, you may also have to set the public URI basePath: $app->setBasePath('/different/path');

1 Like