Intermittent failure to load images - ERR_CONTENT_LENGTH_MISMATCH

1.9k views Asked by At

The problem

My website fails to load random images at random times. Intermittent failure to load image with the following error in console:

"GET example.com/image.jpg net::ERR_CONTENT_LENGTH_MISMATCH"

Image either doesn't load at all and gives the broken image icon with alt tag, or it loads halfway and the rest is corrupted (e.g. colors all screwed up or half the image will be greyed out).

Setup

Litespeed server, PHP/mySQL website, with HTML, CSS, Javascript, and JQuery.

Important Notes

  • Problem occurs on all major web browsers - intermittently and with various images.
  • I am forcing UTF-8 encoding and HTTPS on all pages via htaccess.
  • Hosting provider states that all permissions are set correctly.
  • In my access log, when an image fails to load, it gives a '200 OK' response for the image and lists the bytes transferred as '0' (zero).
  • It is almost always images that fail to load but maybe 5% of the time it will be a CSS file or Javascript file.
  • Problem occurred immediately after moving servers from Apache to Litespeed and has been persistent over several weeks.
  • Gzip and caching enabled.
4

There are 4 answers

1
Zahid Riaz On

This error is definite mismatch between the data that is advertised in the HTTP Headers and the data transferred over the wire.

It could come from the following:

  1. Server : If a server has a bug with certain modules that changes the content but don't update the content-length in the header or just doesn't work properly.
  2. Proxy : Any proxy between you and your server could be modifying the request and not update the content-length header.

This could also happens if setting wrong content-type.

As far as I know, I haven't see those problem in IIS/apache/tomcat but mostly with custom written code. (Writing image yourself on the response stream)

It could be even caused by your ad blocker.

Try to disable it or adding an exception for the domain from which the images come from.

0
jobeard On

Suggest accessing the image as a discrete url using cURL, eg php testCurlimg >image.log 2>&1 to see exactly what is being returned by the server. Then you can move upon level to test the webpage php testCurlpg >page.log 2>&1 to see the context for mixed data

0
Collin Krawll On

I just ran into this same ERR_CONTENT_LENGTH_MISMATCH error. I optimized the image and that fixed it. I did the image optimization using ImageOptim but I'm guessing that any image optimization tool would work.

0
Drew Borell On

Had this problem today retrieving images from Apache 2.4 when using a proxy I wrote in php to provide a JWT auth gateway for accessing a couchdb backend. The proxy uses php fsockopen and the fread() buffer was set relatively low (30 bytes) because I had seen this value used in other peoples work and I never thought to change it. In all my failing JPG (JFIF) images I found the discrepancy in the original versus the image served was a series of crlf that matched the size of the fread buffer. Increased the byte length for the buffer and the problem no longer exists.

In short, if your fread buffer streaming the image is completely full of carriage returns and line feeds, the data gets truncated. This possibly also relates to the post from Collin Krawll as to why image optimization resolved that problem.