Jeremy Felt

Clear nginx Cache in Vagrant

Fooled you. You think that cache is the problem, but it’s not.

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:

Responses and reactions

Mentions

Replies

Phil replied on 

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.

PrateekGera replied on 

Thanks this worked for me :)

P.S To reflect the changes after setting sendfile off, just service ngnix restart did not work for me.

Make sure you due vagrant halt and then restart it using vagrant up.

GoodDay.

Hardy replied on 

from where i can found my NGix.conf file. i use vagrant and centos 6.5 Virtaul mechine i m not able to find such file. please help me

Marcin K replied on 

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.

      Marcin K replied on 

      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.

        Charles Chi-Hua Chen replied on 

        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:

        config.vm.synced_folder "./", "/vagrant", type: "nfs", mount_options: ["nolock", "vers=3", "tcp"]

Danil Golovachev (@danithecake) replied on 

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

Miguel Castellanos replied on 

Hello,

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.

Any help would be appreciated.

Miguel

    ThomasRedstone replied on 

    Hi Migual,

    I had the same problem, even after a vagrant halt;vagrant up, but as soon as I changed the cached file (just creating a new line will do), it worked!

    I guess the cache isn't just in the location you thought, and it may still keep using it until there are changes on disk. Very odd behaviour!

dan replied on 

I'll add another thanks for this post. Fortunately I found it rather quickly after using curl and verifying that nginx served the CSS files oddly, while going directly to Laravel worked fine.

Carlos replied on 

I'm sorry if you think this is thank-you spam. But i must do it: From my soul, a sincerest THANK YOU. I was really about to crash my laptop into pieces. THANK YOU, good soul.

Mathias Hellquist replied on 

With the risk of sounding like spam: This was the best blog post I've read in a long time. You just saved me many hours and most of my hair. Thanks. Thought I was going crazy. :)

Chris replied on 

Thank you! I've spent forever trying to sort this out - disabled all caching in nginx and even re-provisioned by vagrant box. What a strange issue.

K.Adam White replied on 

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!

aditya menon replied on 

Omigod. This page saved me *again*! Just now! Thank you!

I'll probably never forget that sendfile thing now, it will spring first thing to my mind the moment I see weird errors that have no explanation.

Leave a Reply

Your email address will not be published. Required fields are marked *

The only requirement for your mention to be recognized is a link to this post in your post's content. You can update or delete your post and then re-submit the URL in the form to update or remove your response from this page.

Learn more about Webmentions.