Fix 404 Not Found Errors: Complete Guide for Nginx, WordPress, APIs & More
Comprehensive troubleshooting guide for 404 Not Found errors across web servers, applications, and APIs. Step-by-step fixes with exact commands.
- 404 errors occur when the web server cannot locate the requested resource due to misconfigured routing, missing files, or incorrect URL patterns
- Common causes include nginx configuration issues, PHP-FPM problems, incorrect document root settings, and application-specific routing failures
- Most 404 errors can be resolved by checking server configuration files, verifying file permissions, and ensuring proper URL rewriting rules
| Method | When to Use | Time | Risk |
|---|---|---|---|
| Check Configuration Files | Server-wide 404 errors | 5-15 minutes | Low |
| Restart Web Services | After config changes | 2-5 minutes | Medium |
| File Permission Fix | Specific files/directories | 3-10 minutes | Low |
| URL Rewrite Rules | Application routing issues | 10-30 minutes | Medium |
| Complete Reinstall | Corrupted installations | 30-60 minutes | High |
Understanding the Error
404 Not Found errors are HTTP status codes indicating that the web server cannot locate the requested resource. This comprehensive guide covers troubleshooting across multiple platforms including Nginx, Apache, WordPress, Laravel, and various cloud services.
Common Error Messages You'll See
- Nginx: "404 Not Found nginx/1.18.0 (Ubuntu)"
- Apache: "The requested URL was not found on this server"
- WordPress: "The page you requested could not be found"
- API Responses: "404 Not Found - The resource requested could not be found"
- Laravel: "Sorry, the page you are looking for could not be found"
Step 1: Initial Diagnosis
Check Server Status and Logs
First, verify your web server is running and examine error logs for clues:
# Check nginx status
sudo systemctl status nginx
# Check Apache status
sudo systemctl status apache2
# View recent error logs
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/apache2/error.log
Verify File Existence and Permissions
Confirm the requested file exists and has correct permissions:
# Check if file exists
ls -la /var/www/html/requested-file.php
# Check directory permissions
ls -la /var/www/html/
# Fix common permission issues
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
Step 2: Nginx-Specific Troubleshooting
Configuration File Issues
Nginx 404 errors often stem from incorrect server block configurations:
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
# Critical: Ensure location blocks are correct
location / {
try_files $uri $uri/ =404;
}
# For WordPress/Laravel applications
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP processing
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
Test and Reload Configuration
# Test nginx configuration
sudo nginx -t
# Reload if test passes
sudo systemctl reload nginx
# Full restart if needed
sudo systemctl restart nginx
Common Nginx 404 Fixes
Problem: WordPress permalinks not working
location / {
try_files $uri $uri/ /index.php?$args;
}
Problem: Laravel routes returning 404
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Problem: API endpoints not found
location /api {
try_files $uri $uri/ /index.php?$query_string;
}
Step 3: WordPress-Specific Solutions
Permalink Structure Issues
WordPress 404 errors often relate to permalink problems:
Reset Permalinks: Go to Settings > Permalinks in WordPress admin, then click "Save Changes"
Check .htaccess File:
# WordPress .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
- Verify WordPress Configuration:
// wp-config.php
define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');
WordPress with Nginx Configuration
server {
listen 80;
root /var/www/html;
index index.php;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Step 4: Laravel Application Fixes
Nginx Configuration for Laravel
server {
listen 80;
root /var/www/laravel/public;
index index.php;
server_name laravel.local;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
Laravel Route Issues
Check your Laravel routes and ensure proper configuration:
# Clear route cache
php artisan route:clear
# List all routes
php artisan route:list
# Clear all caches
php artisan cache:clear
php artisan config:clear
php artisan view:clear
Step 5: API and Application-Specific Fixes
REST API 404 Errors
For API endpoints returning 404:
# Test API endpoint
curl -X GET http://localhost/api/endpoint -v
# Check API routes
php artisan route:list | grep api
Node.js/Express Applications
// Express.js 404 handling
app.use((req, res, next) => {
res.status(404).json({
error: 'Not Found',
message: 'The requested resource could not be found'
});
});
Angular Routing Issues
For Angular applications with routing problems:
# Angular nginx config
location / {
try_files $uri $uri/ /index.html;
}
Step 6: Cloud Platform Solutions
Azure Application Gateway V2
For Microsoft Azure Application Gateway 404 errors:
- Check backend pool health
- Verify URL path maps
- Review request routing rules
- Ensure backend targets are accessible
AWS and Other Cloud Services
# Check AWS EC2 security groups
aws ec2 describe-security-groups --group-ids sg-xxxxxxxxx
# Verify S3 bucket policy for static websites
aws s3api get-bucket-policy --bucket your-bucket-name
Step 7: Database and CMS Issues
Magento 2 404 Errors
# Magento 2 fixes
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex
php bin/magento setup:upgrade
Database Connection Issues
Verify database connectivity:
# Test MySQL connection
mysql -u username -p -h localhost database_name
# Check database status
sudo systemctl status mysql
Step 8: Advanced Debugging
Enable Debug Logging
# Nginx debug logging
error_log /var/log/nginx/debug.log debug;
# Apache debug logging
LogLevel debug
ErrorLog /var/log/apache2/debug.log
Network and DNS Issues
# Test DNS resolution
nslookup example.com
dig example.com
# Check network connectivity
ping example.com
telnet example.com 80
SSL/HTTPS Configuration
# HTTPS redirect causing issues
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
# SSL configuration
}
Prevention and Best Practices
Regular Maintenance
- Monitor Error Logs: Set up log monitoring and alerting
- Test After Changes: Always test configuration changes in staging
- Backup Configurations: Keep backups of working configurations
- Documentation: Document custom URL patterns and routing rules
Performance Considerations
# Monitor server performance
top -p $(pgrep nginx)
htop
iostat -x 1
By following this systematic approach, you can effectively diagnose and resolve 404 Not Found errors across various platforms and configurations. Remember to always test changes in a staging environment before applying them to production systems.
Frequently Asked Questions
#!/bin/bash
# Comprehensive 404 Error Diagnostic Script
echo "=== 404 Error Diagnostic Tool ==="
# Check web server status
echo "\n1. Checking web server status..."
if command -v nginx > /dev/null; then
echo "Nginx status:"
sudo systemctl status nginx --no-pager -l
echo "\nNginx configuration test:"
sudo nginx -t
fi
if command -v apache2 > /dev/null; then
echo "Apache status:"
sudo systemctl status apache2 --no-pager -l
echo "\nApache configuration test:"
sudo apache2ctl configtest
fi
# Check error logs
echo "\n2. Recent error log entries:"
if [ -f /var/log/nginx/error.log ]; then
echo "Nginx errors (last 10 lines):"
sudo tail -10 /var/log/nginx/error.log
fi
if [ -f /var/log/apache2/error.log ]; then
echo "Apache errors (last 10 lines):"
sudo tail -10 /var/log/apache2/error.log
fi
# Check document root and permissions
echo "\n3. Document root analysis:"
DOC_ROOT="/var/www/html"
if [ -d "$DOC_ROOT" ]; then
echo "Document root exists: $DOC_ROOT"
echo "Permissions:"
ls -la "$DOC_ROOT"
echo "\nOwnership:"
stat -c "%U:%G" "$DOC_ROOT"
else
echo "Document root missing: $DOC_ROOT"
fi
# Test common endpoints
echo "\n4. Testing common endpoints:"
echo "Testing localhost..."
curl -I http://localhost 2>/dev/null | head -1
echo "Testing index.php..."
curl -I http://localhost/index.php 2>/dev/null | head -1
# Check PHP-FPM if available
if command -v php-fpm > /dev/null; then
echo "\n5. PHP-FPM status:"
sudo systemctl status php*-fpm --no-pager -l
fi
# Network connectivity
echo "\n6. Network diagnostics:"
echo "Listening ports:"
sudo netstat -tlnp | grep -E ':(80|443|8080)'
echo "\n=== Diagnostic Complete ==="
echo "\nCommon fixes to try:"
echo "1. sudo systemctl restart nginx (or apache2)"
echo "2. sudo chown -R www-data:www-data /var/www/html/"
echo "3. sudo chmod -R 755 /var/www/html/"
echo "4. Check application-specific configuration files"Error Medic Editorial
Our team of senior DevOps engineers and SREs brings over 50 years of combined experience in troubleshooting web server issues, application deployments, and cloud infrastructure problems across enterprise environments.