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.
- https://github.com/mitchellh/vagrant/issues/351#issuecomment-1339640
- http://smotko.si/nginx-static-file-problem/
- http://stackoverflow.com/questions/9866106/nginx-on-virtualbox-ubuntu-guest-serves-stale-content
- http://stackoverflow.com/questions/6236078/how-to-clear-the-cache-of-nginx
Or, even better – more detail about sendfile itself and other common nginx pitfalls:
Responses and reactions
Mentions
Replies
If only I could send this article to read by myself one hour ago…
Super weird, thanks for sharing, you have saved me a lot of time
Thanks! Saved my day ;)
Thanks!
Big thanks
Thank you very much
thank you for the share. it help me save time
Thank you! Struggled with this for a while.
THANK YOU!!! i have been scratching my head for almost 2 hours for this.
You Saved my life.
Thanks a lot.
I love you
I still love you
Cool~ Thank your so much!
Man, really... you saved me! Thank you so much!
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.
Thanks!!! You save my life!
Thanks dude! Works like charm
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.
"Thank you" - sincerely from my laptop.
Had this issue while running nginx inside a docker container, thanks!
From the bottom of my heart: Thank you so much!
THANKS! THANKS! THANKS! THANKS!
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
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.
That's a bummer. Any chance there is another nginx config file overriding the setting?
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:
config.vm.synced_folder "./", "/vagrant", type: "nfs", mount_options: ["nolock", "vers=3", "tcp"]
Perfect, this worked! I'm surprised so few people run into this specific problem
I'm subscribed to the comments on here. If you had a penny for each of the thanks you received... Why you'd be an Internet millionaire! :D
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
Thanks so much. You saved my day. I've spent few hours on this problem.
It confused me houres, Tanks!
Thank you VERYYYYYYYYYYYYYYYY MUCH!!!!!!!!!!!!!!!!!!!!!!
Thanks!!! You saved my life.
You literally saves hours of mine. Thank you! \m/
Thank you!
Thanks, I love you!!!! After hours of troubleshooting this is what it was. I've been testing out Docker and was almost convinced something was up with the container
The link to the nginx pitfalls website is incorrect. Thanks for the comprehensive post.
Perfect! Thank you!
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!
It worked for me! Thank you
I tried several search terms to find a solution to this misery!
By: Google Translate
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.
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.
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. :)
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.
Thanks!
Thank you :)
You can fix the issue with:
sudo sed -i 's/sendfile on/sendfile off/g' /etc/nginx/nginx.conf
Thank you, you've just stopped us wasting a load more time...
Thank you! I've spend a full day scratching my head to figure out what the hell is going on.
Thank you so much, this was bugging me all afternoon!
Thanks! Exactly what I was looking for.
Thank you, that was very helpful!
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!
Spent an hour trying to figure it out with mycoworker, then stumbled upon your post :) Thanks!
[...] Clear Nginx Cache in Vagrant [...]
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.
Thank you :)
Thank you for this. I won't say the wall doesn't have any head-sized impressions, but you saved it from many more.
You there, sir. Yes, you. You saved my life, thank you.
The author just saved a Brazilian soul! Thanks man!
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.