Apache or Nginx prints gibberish at the end of static file

or prints gibberish

If you struggled for a while trying to figure out why apache or nginx crops the content of your CSS files or it adds a bunch of gibberish at the end of the files, fear no more. The problem can be solved by simply turning off sendfile?

Webserver gibberish

Note the last line.

Apache fix

For Apache, head over to /etc/httpd/conf/httpd.conf or /etc/apache2/httpd.conf or wherever your main conf file is located and enable the set EnableSendfile to off.

EnableSendfile off

The value is most probably already there, all you have to do is to un-comment it out.

Nginx fix

Similarly, on Nginx head over to nginx.conf located under /etc/nginx/ or /usr/local/etc/nginx/ or /usr/local/etc/nginx/conf/ and set the directive sendfile to off.

server {
    ...
    sendfile off;
}

The value is most probably already there, all you have to do is to un-comment it out.

Now restart your Web server and Voila!

What causes the problem.

The problem is sendfile(2). What is sendfile you say? Sendfile is a system call (handled by the OS) to send a file at a much faster speed. To transfer a file, it copies its content directly into the TCP connection thus avoiding any overhead.

When it works it is amazing. I even wrote about how you can use of that feature to serve files with your favorite scripting language at a much faster speed.

When you use it under a VM like VirtualBox, chances are it will fail. The VM has some slightly different implementation of the network connection and file system that cause it to be inconsistent. In my case I was serving files from Apache Webserver on a networked file system. Even though it was in the same machine, it was tunneling through VirtualBox and caused it to fail.

Now go on and spread the word.


Comments

Let's hear your thoughts

For my eyes only

There are no comments added yet.