-5

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

1
  • 2
    If there is a specific problem with this php router, why hide the relevant lines of code from the question? Off-site resources are commonly off-topic on SO as the questions cannot stand for itself. Commented Nov 14 at 17:37

2 Answers 2

0
location ^~ /api {
    alias 'path_to_backend';
    index index.php
    try_files $uri /index.php$is_args$args;
    # etc.

I didn't quite understand the essence of your problem, but apparently you can't follow the path. domain.com/api/**** and you refuse to domain.com try this implementation, it worked for me.

Sign up to request clarification or add additional context in comments.

Comments

0

rtrim($viewPath, '/') . '/'; // Ensure trailing slash <--- Have you tried rtrim on the /?

Also, do you have a .htaccess file that could be overwriting what your code is trying to accomplish?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.