Disable sendfile when using Nginx + Vagrant

Source: http://jeremyfelt.com/code/2013/01/08/clear-nginx-cache-in-vagrant/

Scenario 1… You installed Vagrant with VirtualBox on your local machine and have a sweet nginx setup going as your development environment. You made a few changes to a CSS file and the new style is not reflecting on the page. You try saving the file again in your text editor, no go. You look at the file on the server, it’s cool. You restart the nginx service, still no change. You restart the services for php5-fpm and memcached, maybe even mysql… no go.

Something has captured this file in cache and is not letting go!

Scenario 2… Same setup. You made a few changes to a JS file and the script doesn’t seem to be working. Must be a caching issue. You try saving the file again, look at the file on the server, restart nginx, restart everything. Finally look at the console in your browser and see some kind of random error.

Sooner or later, with one of these files, you open it up and see these:

�����������������

What the what? It’s an encoding issue? Not a caching issue? Or it’s a… wait, what?

Hopefully you haven’t spent too much time trying to figure this out before stumbling on a site like this one that tells you the only change necessary is a simple line in your nginx config file.

sendfile off;

Find the spot in your assorted nginx config files that says ‘sendfile on’ and change it to ‘sendfile off’.

Sendfile is used to ‘copy data between one file descriptor and another‘ and apparently has some real trouble when run in a virtual machine environment, or at least when run through Virtualbox. Turning this config off in nginx causes the static file to be served via a different method and your changes will be reflected immediately and without question – or black question mark diamond thing.

Hope that saves you a minute.

For further reading, consider those that have stumbled on the same problem before.

Or, even better – more detail about sendfile itself and other common nginx pitfalls:

4 Replies to “Disable sendfile when using Nginx + Vagrant”

  1. I had this issue as well and thought the sendfile off; option didn’t work, however it seems like the browser had just cached the old file locally and checking with an incognito/other browser showed updating content. A cache purge on my primary browser then (so far) is reflecting updates instantly.

    Hindsight sometimes makes me feel like I shouldn’t have spent 10 minutes digging deeper than I needed to 🙂

    CCM

  2. What I wouldn’t give to be one of the lucky majority this worked for! It looks to be the right solution but I wonder if things have moved on since that advice was given, as this no longer works. No amount of setting “sendfile off| anywhere in any nginx config file works, I’m afraid. I’m sure it used to.

    If I find a solution, I’ll drop another comment in, as this is driving me bonkers. But for those it _does_ work for, I definitely appreciate you putting this up on their behalf, as it’s a massive head-scratcher.

    1. Did you find a solution i seam to be in the same situation. We could be setting it in the wrong place though.

  3. You literally saves hours of mine. Thank you! m/

Leave a Reply