I made this php router, that parses urls in the index.php and calls certain functions. However my when my frontend hits the api, the api block returns the html that the / is supposed to return, its simply falling back.
location /api/ {
try_files $uri /index.php$is_args$args;
# Handle PHP files
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; # Adjust PHP version
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/crumbsnet/api;
fastcgi_param REQUEST_URI $1;
include fastcgi_params;
# CORS headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, X-Requested-With' always;
# Handle preflight requests
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, X-Requested-With';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
}
This is the nginx config block responsible for the api. And here is the api repo if needed