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.

Since a lot of people are probably coming here through the Varying Vagrant Vagrants repository, and it looks like sendfile is now off by default in that project, you may sleep easy: If you’re relying on Varying Vagrant Vagrants for your local development environment setup, this should no longer be a problem if you’re running the latest version!

I am using NGINX 1.6.0-1, I turned off the sendfile option, also turned off the browser’s cache but the files continue to be served from the cache even after restarting NGINX and deleting the whole cache directory.

Thank you Jeremy! I was fighting with this problem half a day. You article has save a lot of my time!
In case someone has apache instead of nginx: “EnableSendFile” is responsible for handle sendfile feature. In openSUSE for example it’s located in /etc/apache2/server-tuning.conf

Running version nginx 1.4.6 and sendfile off is not fixing the issue for me. I restarted nginx – nothing, then restarted vagrant – nothing, modified cached file – nothing.
My application is located in directory mounted by vagrant from OSX and not physically located in vagrant. Maybe this is causing the issue. When I serve application with uwsgi it is all fine, the moment I give control to nginx then I get corrupted files.

There is one main config at /etc/nginx/nginx.conf where i use “sendfile off;”. This config includes app specific configuration (there’s only one). I tried also placing “sendfile off” in app configuration file within server {} section but this didn’t help either.

I had the same problem as you Marcin (setting sendfile to ‘off’ did not fix the problem). You are correct that it has something to do with vagrant’s mounting of the application folder. I had the same setup and debugged for 3-4 hours only to realize that this problem happens when i edit the application files from the host (when i edit within vagrant’s vm it is totally fine).

Then i googled around for solutions for mounting the NFS correctly. I found this configuration (for Vagrantfile) works with editing application files from the host computer:

Thank you, you just stopped my migraine.
This article will soon be 3 years old ; it boggles my mind that puphpet and/or Vagrant still configure nginx with “sendfile” to “on”… or don’t configure it to “off” it is on by default.
I was ready to turn back to xamp if I didn’t solve this issue, thank you again.