I'm trying to add a new location block with an alias directive that are based on a dynamic URI to access different APIs. Right now I can do this manually adding each location block but was wondering if it could be mapped by using REGEX.
The problem is that it's returning a 404 error. I'm running a laravel sub app in a different folder on my server.
Any clues?
**
WORKING MANUALLY
location /api/product {
alias /path/to/api/product/public;
try_files $uri $uri/ @rewrite;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location @rewrite {
rewrite /api/product/(.*)$ /api/product/index.php?/$1 last;
}
ERROR 404 / No input file specified
location ~ /api/(.*) {
alias /path/to/api/$1/public;
try_files $uri $uri/ @rewrite;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location @rewrite {
rewrite /api/(.*)/(.*)$ /api/$1/index.php?/$2 last;
}
MORE TESTS
A
URL: app.tdl/api/tweets
Result: 'Index of /api/tweets/'
Output of $request_filename: /home/vagrant/code/Gateway/modules/tweets/app
location /api/tweets {
alias /home/vagrant/code/Gateway/modules/tweets/app;
autoindex on;
}
B
URL: app.tdl/api/tweets
Result: Nginx's 404
Output of $apiName: tweets
Output of $request_filename: /home/vagrant/code/Gateway/modules/tweets/app
location ~ "/api/(?<apiName>[^/]+)" {
alias "/home/vagrant/code/Gateway/modules/$apiName/app" ;
autoindex on;
}