HTTP Error Codes: How to Troubleshoot Common Ones
In this article
HTTP error codes are the web server's way of telling you something went wrong with your request. They range from the common 404 (Not Found) to the more cryptic 502 (Bad Gateway). This guide walks you through the most frequent errors you'll encounter, what they mean, and the step-by-step process to resolve them. Whether you're a website owner, developer, or just a curious user, you'll learn how to diagnose and fix these issues quickly.
What You'll Need
- Access to the browser's developer tools (usually F12 or right-click → Inspect)
- Command line or terminal access (for curl commands)
- If you manage the server: SSH access or hosting control panel login
- A text editor for editing configuration files (if applicable)
Step-by-Step
- Identify the exact error code. The first step is to know which error you're dealing with. Look at the browser's error page, the network tab in developer tools, or the server logs. The code is a three-digit number, and the first digit indicates the category: 1xx (informational), 2xx (success), 3xx (redirection), 4xx (client error), and 5xx (server error). We'll focus on the most common 4xx and 5xx errors.
- For 400 Bad Request: This means the server couldn't understand your request due to malformed syntax. Check the URL for typos or invalid characters. If you're submitting a form, ensure all required fields are filled correctly. Clear your browser cache and cookies, then try again. If you're a developer, inspect the request payload for JSON or form data errors.
- For 401 Unauthorized: This indicates you need to authenticate. If you're a user, check if you're logged in. Log out and log back in, or clear your browser's saved passwords. If you're a developer, verify your API keys or tokens are correct and not expired.
- For 403 Forbidden: The server understood your request but refuses to fulfill it. This is often due to file permissions. If you manage the server, check that the file or directory permissions are correct (e.g., 644 for files, 755 for directories). Ensure the .htaccess or nginx config isn't blocking your IP or user agent. If you're a user, try accessing the resource from a different network or device to rule out IP blocking.
- For 404 Not Found: The resource doesn't exist. Double-check the URL for typos. If you're a site owner, check if the page was moved or deleted. You can set up a redirect from the old URL to the new one. Use a tool like a broken link checker to find internal links that might be pointing to non-existent pages.
- For 408 Request Timeout: The server timed out waiting for your request. If you're a user, refresh the page, or try again later. If you manage the server, increase the timeout value in your server configuration (e.g.,
proxy_read_timeoutin nginx orTimeoutin Apache). Also, check if your server is overloaded. - For 429 Too Many Requests: You've sent too many requests in a given time. If you're a user, slow down your requests and wait a bit. If you're a developer, implement exponential backoff in your client. If you manage the server, you can adjust the rate limiting rules in your web server or application.
- For 500 Internal Server Error: This is a generic server-side error. Check the server error logs (e.g.,
/var/log/apache2/error.logor/var/log/nginx/error.log). Common causes include PHP syntax errors, database connection failures, or permission issues. If you have a backup, try restoring the last known working version. If you're using a CMS, disable plugins one by one to find the culprit. - For 502 Bad Gateway: A server acting as a gateway received an invalid response from the upstream server. This often happens with reverse proxies. Check if the backend service (like PHP-FPM or a Node.js app) is running. Restart the service if needed. Also, check for network issues between the proxy and the backend.
- For 503 Service Unavailable: The server is temporarily unable to handle the request, often due to maintenance or overload. If you're a user, wait a few minutes and try again. If you manage the server, check if you have a maintenance page enabled. If it's overload, increase server resources or implement load balancing.
- For 504 Gateway Timeout: A gateway didn't receive a timely response from the upstream server. This is similar to 502 but specifically a timeout. Check the upstream server's response time and increase the timeout settings on the proxy. Also, check for slow database queries or external API calls that might be causing the delay.
Common Mistakes
- Ignoring the exact error code: Treating all 5xx errors the same can lead you down the wrong path. A 503 is different from a 500.
- Not checking server logs: The logs often have the precise error message. Skipping this step means guessing.
- Clearing cache when it's a server issue: While clearing cache helps with 400 and 404, it won't fix a 500 error.
- Forgetting to test after a fix: Always verify the fix by reloading the page or running a curl command.
Tips
- Use the browser's developer tools (Network tab) to see the exact request and response headers. This can reveal details like server type and error messages.
- Use
curl -Ito see only the response headers for a quick check. - For developers, implement proper error handling in your application to return meaningful error messages.
- Set up monitoring alerts for 5xx errors on your server.
- Keep your server software and frameworks up to date to avoid known bugs.
Troubleshooting
If the steps above don't resolve the issue, consider the following:
- Check DNS: Sometimes the error is not from your server but from a DNS resolution issue. Use
nslookupordigto verify your domain resolves correctly. - Test with a different browser or device: This helps determine if the issue is local to your browser or network.
- Disable browser extensions: Extensions can interfere with requests, causing 4xx errors.
- Check firewall or security plugins: A web application firewall (WAF) might be blocking legitimate requests, resulting in 403 or 429 errors.
- Look at the server's resource usage: If the server is out of memory or disk space, it can cause 500 errors. Use tools like
topordf -hto check.
Frequently Asked Questions
What is the difference between 401 and 403?
401 means you are not authenticated (you haven't logged in or provided valid credentials). 403 means you are authenticated but you don't have permission to access the resource.
Why do I see a 404 error even though the page exists?
This can happen if the URL is wrong (case sensitivity, trailing slash), if the page is cached with a 404 status, or if there's a rewrite rule that's misconfigured.
How can I find out what's causing a 500 error?
Check the server error log. It usually contains a stack trace or a detailed message. If you don't have access, try enabling WP_DEBUG if you're using WordPress, or check the application's log file.
What should I do if I get a 503 error during a traffic spike?
First, confirm it's a traffic spike. Then, consider scaling up your server resources, using a CDN to offload static content, or implementing caching. If you have a load balancer, check its health checks.
Can a 502 error be caused by my internet connection?
Usually not. A 502 is a server-side error, meaning the server you're connecting to is having trouble. However, if you're using a VPN or proxy, it might be that intermediary causing the issue.
How do I fix a 429 error on my website?
If you're a user, wait and retry. If you're the site owner, you can increase the rate limit or make it more lenient. Also, check if any of your plugins or scripts are making too many requests.