How to solve Apache error 502 “Bad Gateway” when updating Alfresco node content

In case you are using Apache as a proxy for your Alfresco Content Service Tomcat, you may face this unexpected error after trying to upload a medium-large file to Alfresco.

Even if you are sending the username/password or the ticket along with the request, for some reason Tomcat returns a 401 response for requests that take more than usual, so uploading medium-large size files to Alfresco return this error (however, calls with small files work fine)

The major problem from this situation is the way that Apache is translating the 401 error into 502 Bad Gateway response, which is misleading and confusing in order to solve this issue.

Solution

Even if you are sending the username/password or the ticket along with the request, make sure you add the following header to solve the problem:

Authorization: Basic <Alfresco Credentials in Base64 Encoding>

If using Java code:

HttpPut put = new HttpPut(uri);
put.setHeader(org.apache.http.HttpHeaders.AUTHORIZATION, "Basic <Alfresco Credentials in Base64 Encoding>");

If you need to troubleshoot this issue further, you can log requests headers on Apache to verify the information received, and beyond that, you can also log requests headers on Tomcat to check information that finally gets into Tomcat and the status it returns.