Why are my CSS/JS assets returning 404 on Nginx?

Why are my CSS/JS assets returning 404 on Nginx?

Answer: Ensure the public folder is set as your Nginx root, php artisan storage:link is run for uploaded assets, and permissions on the public directory are correct.

Common Causes:

  • Incorrect document root in Nginx configuration
  • Missing storage symlink
  • Incorrect file permissions
  • Asset compilation issues
  • Cache problems

Solutions:

1. Check Nginx Configuration:

Ensure your document root points to the public directory:

server {
    root /path/to/taskify/public;
    # ... rest of configuration
}

2. Create Storage Symlink:

php artisan storage:link

3. Set Correct Permissions:

chmod -R 755 /path/to/taskify/public
chown -R www-data:www-data /path/to/taskify/public

4. Build Assets:

npm run build
# or for development
npm run dev

5. Clear Caches:

php artisan cache:clear
php artisan config:clear
php artisan view:clear

Verification:

Check if assets are accessible:

  • Visit your domain directly
  • Check browser developer tools for 404 errors
  • Verify file paths in HTML source