1

I'm not getting through this.

My server is app.local and I need to respond to:

http://app.local/api/v1/

I need to configure my nginx to serve files placed in:

/app/api/code

So the filesystem is not reflecting the http request form.

CURRENT VERSION

server {
    server_name app.local;
    index index.php;

    location /api/v1 {
        alias /app/api/v1/code;

        try_files $uri /api/v1/index.php$is_args$args;

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass api-v1-php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }
}

Ok, if I remove the outer try_files it seems to find the index.php, BUT i lose some functionality (I need to redirect every path to the index handler). How can I solve this? Is this a bug?

SOLUTION

This post had the solution: https://stackoverflow.com/a/35102259/2373113

UPDATED VERSION

server {
    server_name app.local;
    index index.php;

    location /api/v1/ {
        alias /app/api/v1/code/;

        try_files $uri $uri/ /api/v1//api/v1/index.php$is_args$args;

        location ~ /api/v1/.+\.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass api-v1-php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }
}
Jumpa
  • 4,319
  • 11
  • 52
  • 100
  • Have you tried `try_files $uri /app/api/code/index.php$is_args$args;` ? You should first try to solve the issue for non-PHP files, and only then move on to the PHP case. – IVO GELOV Jan 25 '21 at 07:31
  • I've tried your solution, same error now looking for /etc/nginx/html/app/api/code/index.php path. – Jumpa Jan 25 '21 at 07:34
  • Try with text files instead of PHP files. – IVO GELOV Jan 25 '21 at 07:37
  • I've tried index.html and is correctly served. – Jumpa Jan 25 '21 at 07:38
  • The correct URI for `index.php` is `/api/v1/index.php` and not `/index.php`. Both `location` and `alias` should end with a `/` or neither end with a `/`. Use `fastcgi_param SCRIPT_FILENAME $request_filename;` when using `alias`. – Richard Smith Jan 25 '21 at 07:39
  • @RichardSmith I've edited my answer, I'm getting "invalid number of arguments in "fastcgi_param" directive"... – Jumpa Jan 25 '21 at 07:49
  • Ok, now change the second `try_files` (the one inside the 2nd location block) in the same way. – IVO GELOV Jan 25 '21 at 07:49
  • @IVOGELOV sorry can you please write the line? Shoud I delete the 404? – Jumpa Jan 25 '21 at 07:51
  • `try_files $uri /app/api/code/$uri =404;` but I think you can simply include your alias in this 2nd location block and see if the current `try_files` works – IVO GELOV Jan 25 '21 at 07:55
  • @IVOGELOV Edited, this is not working I'm getting the same error... – Jumpa Jan 25 '21 at 08:05
  • You should either add the `alias` or change the `try_files` - not both. – IVO GELOV Jan 25 '21 at 09:03
  • I've tried even singularly, same error. – Jumpa Jan 25 '21 at 09:06
  • You have an invalid `fastcgi_param` directive. It is supposed to be setting a value for `SCRIPT_FILENAME`. Anyway, also see [this answer](https://stackoverflow.com/a/42467562/4862445). – Richard Smith Jan 25 '21 at 09:34
  • @RichardSmith you're right I've fixed it. But the real problem here I think it's the outer try_files because removing it, it seems to work...any downside removing it? – Jumpa Jan 25 '21 at 09:36
  • You have also added another `alias` directive into the inner block which will probably break everything. – Richard Smith Jan 25 '21 at 09:40
  • @RichardSmith I've cleaned up and posted the latest version...could you please review it? I need to preserve the try_files behavior. – Jumpa Jan 25 '21 at 09:45
  • You have a typo, the `location` uses `v1` but `try_files` uses `v1.0`. – Richard Smith Jan 25 '21 at 10:03
  • Sorry, I've fixed it. – Jumpa Jan 25 '21 at 10:05

0 Answers0