When someone tries to access a web server or an application, every server receives an HTTP request and the server responds to this request with a particular HTTP status code. The HTTP status codes consist of three digits and they are classified into five different classes. The class of any HTTP status code can be easily determined by its first digit.
HTTP Status Code Classification:
- 1xx : Informational
- 2xx : Success
- 3xx : Redirection
- 4xx : Client Error
- 5xx : Server Error
This guide provides ways for troubleshooting the most commonly experienced HTTP error codes – i.e. the HTTP error codes of status codes 4xx and 5xx. There are many instances that can cause the web server to respond with a particular HTTP error code. Here you will get more information on the most common causes of HTTP error codes and their solutions.
An Overview On Client And Server Process
The HTTP status codes ranging from 400 to 499 and client errors are caused due to HTTP requests made by a user client – here user client means a web browser or any other HTTP client. Although these are client related errors, it is important to know which error code is being sent to the user in order to determine if that particular issue can be fixed through any modifications in the server configuration.
HTTP status codes ranging from 500 to 599 and server errors are caused by a web server when it knows that a particular error has occurred or if the server is not able to process a request.
Common Troubleshooting Tips
- When a web browser is used for testing a web server, it is important to refresh the browser after the server modifications are made.
- Analyze the server logs for getting more information about how the server is managing the requests. For example, the web servers like Nginx and Apache create two files known as ‘access.log’ and ‘error.log’ that can be viewed for necessary information.
- It is important to note that the HTTP status code definitions are standardized and implemented by the application that serves the requests. Therefore, the status code that is sent is based on the way in which the server software manages a particular request.
The Most Commonly Encountered HTTP Errors:
400 Bad Request
The 400 status code also known as the ‘Bad Request’ error signifies that the HTTP request that was sent to the server consists of invalid syntax.
Here are some instances when a 400 Bad Request error might arise:
- The user’s cookie that is connected with the site is corrupt. The solution to this issue is cleaning the browser’s cache and cookies.
- Distorted request sent because of a faulty browser.
- Distorted request sent because of a human error while manually creating HTTP requests (when ‘curl’ is incorrectly used).
The 401 status code also known as the ‘Unauthorized’ Error means that a user was accessing a resource that is not authenticated at all or is not correctly authenticated. In this case, the user has to provide the necessary credentials for viewing the protected resource.
An example of this situation is when a 401 Unauthorized error is sent in case a user tries to access a resource that is protected through HTTP authentication. In this situation, the user will get a 401 error code until the valid login credentials are entered to the web server – username and password (the credentials that are mentioned in the .htpasswd file).
The 403 status code also known as the ‘Forbidden’ Error signifies that a valid request was made by the user but the server did not serve the request. This happens because of lack of permission for accessing the valid resource. If you unexpectedly encounter a 403 error, you need to know about some general causes that are mentioned below:
The 403 error usually occurs when a user that is operating the web server process does not have the required permissions for reading the file that is being accessed. Here are some situations for troubleshooting the 403 error:
- The user is trying to access the index file of the web server through – http://www.examplewebsite.com/index.html
- The web server worker process belongs to ‘www-data’ user.
- The index file is located at – /user/share/nginx/html/index.html on the server.
If the user encounters a 403 Forbidden error, it is important to make sure that the www-data user has all the necessary permissions for reading the file. It is important to ensure that the ‘other permissions’ of the file are set to ‘read’. There are many ways for ensuring this, but the command mentioned below will work in this situation:
sudo chmod o=r /usr/share/nginx/html/index.html
Another likely cause of a 403 error is when the .htaccess file is accessed. The .htaccess file can be utilized for denying the access of some resources for some specific IP addresses or ranges. For instance, if a user encounters a 403 error unexpectedly, it is important to ensure that this error is not caused due to any of the .htaccess file settings.
Index File Does Not Exist
If a user tries to access a directory that does not consist of a default index file and if the directory listings are not enabled, the 403 forbidden error will be returned by the web server. For instance if the user is trying to access – http://www.examplewebsite.com/emptydir/ and if there is no index file present in – emptydir directory, the 403 status code error will be returned.
If you want to enable the directory listings, you can do this through your web server configuration.
404 Not Found
The 404 error code also known as the ‘Not Found’ error states that the user can communicate with the server but it is not able to find the requested file or resource.
404 errors are cause due to many different situations. If the user unexpectedly receives the 404 bad request error, here are some important questions to think upon for troubleshooting this error:
- Does the link that is directed the user to the web server consist of any typographical error?
- Has the user entered the wrong URL?
- Is the requested file saved in the correct location on the server? Was the resource shifted or deleted on the server?
- Does the server configuration have the right document root location?
- Does the user incharge of the web server worker process have the necessary privileges to reach the directory where the requested file exists? (Note : Directories must have the read and execute permissions in order to be accessed).
- Is the resource being accessed through some symbolic link? In this case, make sure that the web server is configured for allowing symbolic links.
500 Internal Server Error
The 500 status code also known as the ‘Internal Server’ error means that the server is not able to process the request due to some unidentified reason. At times, this code is displayed when the 5xx errors are more appropriate.
The most common reason for the occurrence of this error is the wrong configuration of the server (example a .htaccess file that is not correctly formed) or if some packages are missing (example when you try to access some PHP file when PHP is not correctly installed).
502 Bad Gateway
The 502 status code also known as the ‘Bad Gateway’ error means that the end server is a gateway or a proxy server and it is not able to receive a valid response from the backend servers that should actually complete the request.
In case the server is a reverse proxy server, like if it is a load balancer, here are some important points that should be analyzed:
- The backend servers where the HTTP requests are forwarded should be properly configured.
- The reverse proxy should be configured correctly with the appropriate backends.
- The network connection between the reverse proxy server and the backend servers should be rightly made. If the servers are communicating through other ports, the firewall should not stop the traffic between them.
- In case your web application is configured to listen on a socket, it is important to ensure that the socket is placed in the right location and it has the required permissions.
503 Service Unavailable
The 503 status code also known as the ‘Service Unavailable’ error means that the server is overburdened or it is under maintenance. This error signifies that the service will be made available after a certain period of time.
If the server is not under any maintenance process, this indicates that the server is not equipped with the required CPU or memory resources for managing all the incoming requests or that the web server has to be configured for allowing more users, threads and processes.
504 Gateway Timeout
The 504 status code also known as the ‘Gateway Timeout’ error means that the server is a gateway or a proxy server and it is not able to get a response from the backend servers within a particular time period.
This error usually occurs in the following situations:
- A poor network connection between the severs.
- The backend server that is executing the request is very slow, it has poor performance.
- The timeout duration of the gateway or the proxy server is very short.
Now that you are aware of the most common HTTP error codes and their solutions, you will be in a better potion to understand and troubleshoot the issues with your applications and web servers.