I am trying to set up an API where I can access API "foobar" through the URL http://my-apis.com/foobar/route. This is what I have so far:

location ~ ^/foobar(/.*)$ {
    root /var/www/mysite/foobar/public;

    ... more fastcgi stuff ...

    fastcgi_param SCRIPT_FILENAME $document_root/index.php$1;
    fastcgi_param PATH_INFO $fastcgi_path_info;

The API is routing to a Slim framework application and currently it successfully routes to the correct index.php, showing an nginx 404/403 whenever the URL does not start with /foobar. However the route passed to Slim (which looks like it's represented by $1 on line 6) is still the full /foobar/route. This means I have to append all all my Slim routes with /foobar, which, although I can use a Slim group, is still a pain. I would like to be able to pass just the /route bit to Slim.

Is there a way I can extract just the wildcard-matched bit of the location directive? Since $1 gives the full route. Alternatively I might be able to do this with some kind of rewrite, but I don't know enough about Slim.

Any help would be much appreciated!


  • Please do not re-post questions you have already asked. – Tero Kilkanen Dec 24 '17 at 2:04
  • @TeroKilkanen This is not a repost.... It's a different question that has followed on from one I asked previously, but the last one is still relevant so I wanted to keep them separate. Forgive me for not polluting the site. Please read questions properly. – Raiden616 Dec 24 '17 at 14:22

SCRIPT_FILENAME is the path to the script and not the original URI. Scripts pick up the path info from PATH_INFO or by decoding the original URI (using REQUEST_URI).

This might work for you:

fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param PATH_INFO $1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.