Disable caching when serving static files with Nginx (for development)

We are using Nginx to serve static files on a development platform. As it is a development platform, we'd like to disable caching so that each change is propagated to the server. The configuration of the VHost is quite simple:

server {
  server_name  static.server.local;
  root /var/www/static;

  ## Default location
  location / {
    access_log        off;
    expires           0;
    add_header        Cache-Control private;
  } 
}

When we access an HTML file (http://static.server.local/test.html), we have no issue: the server returns a code 304 Not Modified as long as the file is not changed, and a 200 OK response with the modified file when the file is changed.
However, it seems to behave differently with a Javascript or a CSS file. Once the file is changed, we get a 200 OK response as expected, but with the old text.
Is there an internal cache mechanism in Nginx that could explain this behaviour? Or some configuration that we should add?

As a side note, here is the header returned by Nginx when the file has been modified (it seems correct):

Accept-Ranges:bytes
Cache-Control:max-age=0
private
Connection:keep-alive
Content-Length:309
Content-Type:text/css
Date:Fri, 13 May 2011 14:13:13 GMT
Expires:Fri, 13 May 2011 14:13:13 GMT
Last-Modified:Fri, 13 May 2011 14:13:05 GMT
Server:nginx/0.8.54

Edit
After trying different settings with the expires directive and Cache-Controlheader, I made some further investigations. In fact, the server is installed on a VirtualBox guest Ubuntu, and data are read from a shared folder that is on the Mac OSX host.
If the file is edited from an IDE (NetBeans) on the host, it seems that changes do not appear whereas if I edit it directly on the guest (using VIM), it is refreshed.
The strange thing is it does not behave similarly with HTML files.
Quite puzzling.

Edit 2 (ANSWER)
Indeed, the origin of the issue was more on the VirtualBox side. Or rather a conflict between VirtualBox and the "sendfile" option of the server.
This link VirtualBox Hates Sendfile gave me the solution: switch the sendfile flag in the server configuration to off:

sendfile  off;

Hope this could also help other person using VirtualBox for development. :)
There are some additional information on the VirtualBox forum.


Solution 1:

Since the answer is somehow hidden in the question - here is the solution for nginx in a VirtualBox environment as standalone answer.

In your nginx config (usally /etc/nginx/nginx.conf) or vhost config file change the sendfile parameter to off:

sendfile  off;

While sendfile is at the heart of Nginx's fame (blazing-fast low-level static file serving efficiency) it might be a bane for local development, e.g. Javascripts that change often and need to be reloaded. Nonetheless Nginx sendfile is smart and probably isn't most people's issue; check your browser's "disable cache" options as well!

Solution 2:

set your expires tag to

expires off;

and it should not set any expires headers at all, it could also be your browser caching files incorrectly

Solution 3:

This is old bug in VirtualBox (see: #819, #9069, #12597, #14920) where vboxvfs seems to have some problems with mmapped access to files which are synched.

This may happen when you edit the file outside of VM, and you expect to see the same change within the VM.

To workaround this problem, you need to disable the kernel sendfile support to deliver files to the client by disabling EnableSendfile option. This is especially trouble for NFS or SMB mounted files.

For Nginx (change in nginx.conf), e.g.

sendfile off;

Similar for Apache (in httpd.conf or in vhosts file), e.g.

<Directory "/path-to-nfs-files">
  EnableSendfile Off
</Directory>

After the change reload the Apache.


Other potential solution is just remember to not edit the files on the host, or try to re-edit the same file, but within the VM.


Another workaround includes dropping the Linux pagecache, e.g.

echo 1 > /proc/sys/vm/drop_caches

Or to clear the caches every second (as per this post), try:

watch -n 1 $(sync; echo 1 > /proc/sys/vm/drop_caches)

Note: Number 1 stands for freeing pagecache, 2 for dentries and inodes, 3 for pagecache, dentries and inodes.


The above problem can be replicated by the following mmap-test program, see: mmap-problem.c.

Solution 4:

This is late, but still marked unanswered, so I will take a stab. Just for giggles, have you tried:

location ~* \.(css|js)$ {
    expires 0;
    break;
}

Haven't tried this myself, but have learned to try this kind of thing with Nginx in a server container from time to time when I have issues similar to this...