Clear nginx Cache in Vagrant

21

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:

21 comments

  1. A vagrant says:

    Thank you for this. I won’t say the wall doesn’t have any head-sized impressions, but you saved it from many more.

  2. aditya menon says:

    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.

  3. K.Adam White says:

    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!

  4. Chris says:

    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.

  5. Mathias Hellquist says:

    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. :)

  6. Carlos says:

    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.

  7. dan says:

    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.

  8. Gustavo Rafael says:

    It worked for me! Thank you
    I tried several search terms to find a solution to this misery!

    By: Google Translate

  9. 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

    • 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!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>