New to NGINX configuration, PHP Routing seems to 404

51 Views Asked by At

I am attempting to build a PHP based web application, however I am running into an issue with, I'd assume, the NGINX Configuration for this.

The main URL (www.example.com/apollo/) works perfectly and does exactly what it needs to do, however any additional pages (**www.example.com/apollo**/home) throws a 404 error

I've attempted to look through past issues that have been similar, and attempted fixes they provided, however it ends up either breaking the app completely, or just not making a difference.

I need it to be able to serve content via the URL passed. I will paste the relevant code below

NGINX Config

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/;

    access_log /var/www/example.com/logs/access.log combined;
    error_log /var/www/example.com/logs/error.log debug;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }

    location ~ /\.ht {
        deny all;
    }

    location /apollo/ {
        auth_basic "Use Your Developer Login To Enter!";
        auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
        alias /var/www/example.com/apollo/public/;
        try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }

    location /api/ {
        alias /var/www/example.com/api/;
        try_files $uri $uri/ /index.php?$query_string;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }
}

PHP Index

$request = $_SERVER['REQUEST_URI'];
$request = preg_replace('#^/apollo/#', '', $request);

ob_start();

switch ($request) {
    case '':
    case 'home':
        require __DIR__ . '/../pages/home.php';
        break;
    case 'summoner':
        require __DIR__ . '/../pages/summoner.php';
        break;
    default:
        http_response_code(404);
        echo 'Page not found';
        break;
}

$content = ob_get_clean();

Hopefully somebody can assist with this, it's probably very simple, i'm just extremely new to working with NGINX

0

There are 0 best solutions below