# Fix for 404 Not Found Errors (nginx)

**Date**: 2026-05-27
**Issue**: 404 Not Found errors on nginx for m.smartscreens.online

## Problem Description

The site was returning 404 errors for all routes including:
- Homepage (`/`)
- API endpoints (`/api/auth/check`, `/api/stats`, etc.)
- Admin panel (`/admin`)

However, the actual content was being served correctly - only the HTTP status code was 404.

## Root Causes

### 1. Router Bug - Root Path Handling
**File**: `src/Middleware/Router.php`

The router was converting the root path `/` to an empty string using `rtrim($uri, '/')`, causing route matching to fail.

```php
// BEFORE (buggy)
$uri = rtrim($uri, '/');  // Converts "/" to ""

// AFTER (fixed)
if ($uri !== '/') {
    $uri = rtrim($uri, '/');
}
```

### 2. Missing Root Route Handler
**File**: `public/index.php`

There was no handler registered for the root path `/`, so the homepage would 404.

```php
// Added to index.php after router initialization
$router->get('/', function() {
    require __DIR__ . '/admin/index.php';
});
```

### 3. Nginx Location Block Order
**File**: `/etc/nginx/conf.d/00-m.smartscreens.online.conf`

The PHP location block was before the root location block, causing routing issues.

**Before**:
```nginx
location ~ \.php$ {
    # PHP handling
}
location ~* \.(css|js|jpg|jpeg|png|gif|webp|mp4|webm|ogg)$ {
    # Static files
}
location / {
    try_files $uri $uri/ /index.php?$query_string;
}
```

**After**:
```nginx
# Static files first
location ~* \.(css|js|jpg|jpeg|png|gif|webp|mp4|webm|ogg|ico|svg|woff|woff2|ttf|eot)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
    try_files $uri =404;
}

# All routes go through index.php
location / {
    try_files $uri /index.php$is_args$args;
}

# PHP handler
location ~ \.php$ {
    fastcgi_pass php_backend;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param REQUEST_METHOD $request_method;
}
```

## Verification Steps

```bash
# Test homepage
curl -s -o /dev/null -w "%{http_code}" https://m.smartscreens.online/
# Should return: 200

# Test API endpoint
curl -s -o /dev/null -w "%{http_code}" https://m.smartscreens.online/api/auth/check
# Should return: 200

# Test admin panel
curl -sL -o /dev/null -w "%{http_code}" https://m.smartscreens.online/admin
# Should return: 200
```

## Monitoring

A monitoring script has been set up at:
- `/home/ashraffarid2010/m.smartscreens.online/monitor.sh`
- Runs every 5 minutes via cron

The script checks:
1. PHP-FPM socket exists at `/tmp/php81.sock`
2. Site returns HTTP 200 status

If issues are detected, it attempts to restart PHP-FPM.

## Related Services

- **PHP Version**: PHP 8.1 (ea-php81)
- **PHP-FPM Socket**: `/tmp/php81.sock`
- **Nginx Config**: `/etc/nginx/conf.d/00-m.smartscreens.online.conf`
- **Site Root**: `/home/ashraffarid2010/m.smartscreens.online/public`
- **Logs**:
  - Nginx access: `/var/log/nginx/m.smartscreens.online.access.log`
  - Nginx error: `/var/log/nginx/m.smartscreens.online.error.log`
  - PHP error: `/home/ashraffarid2010/m.smartscreens.online/public/error_log`
  - Monitor: `/home/ashraffarid2010/m.smartscreens.online/monitor.log`

## Quick Recovery Commands

```bash
# Check PHP-FPM status
ls -la /tmp/php81.sock

# Restart PHP-FPM (cPanel)
/scripts/restartsrv_apache_php_fpm ea-php81

# Restart nginx
systemctl reload nginx

# Test nginx config
nginx -t

# View recent errors
tail -50 /home/ashraffarid2010/m.smartscreens.online/public/error_log
tail -50 /var/log/nginx/m.smartscreens.online.error.log
```
