23

We have this setting in our nginx.conf for quite a while.

sendfile on;

When we have updated a file e.g. /js/main.js and access from browser https://test.com/js/main.js?newrandomtimestamp, it will still load the older version unless we do a full refresh (clear cache) from our browser.

But when we change the settings from sendfile on; to sendfile off; the browser will load the correct version of the updated file.

For our production web server, should we use sendfile on; or sendfile off;? If sendfile on; is required (May for the reason of better caching? Faster performance?) then how to solve the problem mentioned above?

Below is the nginx.conf in our production server, and we are using version 1.7.5:

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections  51200;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    client_max_body_size 8m;
    sendfile        on;
    keepalive_timeout  65;

    real_ip_header X-Forwarded-For;
    set_real_ip_from 0.0.0.0/0;
    large_client_header_buffers 4 32k;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript application/javascript text/css application/xml application/json;
    gzip_vary on;


    include /etc/nginx/conf.d/*.conf;
}
6
  • To make things easier, should we restart nginx every time we have deployed new files to our production server? If we don't want to restart nginx, how else we can clear nginx cache? (assuming if sendfile on; is related to cache) Nov 19, 2015 at 7:59
  • Does your nginx in some kind of virtual environment (like virtualbox)?
    – Alexey Ten
    Nov 19, 2015 at 8:51
  • Our production server is on Amazon EC2 Nov 19, 2015 at 9:08
  • There are several bug reports about sendfile and VirtualBox drive (e.g. virtualbox.org/ticket/819). May be there is similar problem with Amazon.
    – Alexey Ten
    Nov 19, 2015 at 9:18
  • Check out the config settings of open_file_cache as you are hitting this internal cache here. You can disable it completely or reduce the TTL (open_file_cache_valid). You'll find more details here: nginx.org/en/docs/http/… The mentioned issues linked with Virtualbox are because of the specific file system VBOXSF but this should not be the case here. Other known issues are linked to the NFS filesystem which is also here not in place. Feb 21, 2019 at 8:45

3 Answers 3

4

... unless we do a full refresh (clear cache) from our browser.

This in itself, a clear manifestation that the "problem" is on the client-side.

sendfile has nothing to do with caching, only how NGINX buffers / reads the file (trying to stuff the contents directly into the network "slot", or buffer its contents first).

The only reasonable explanation is that your specific browser discards ?newrandomtimestamp as a parameter without value, so it's loading the same cached resource for either example.com?blah and example.com?boo.

If you give it a try, then https://example.com/js/main.js?v=newrandomtimestamp scheme, should give newer content every time.

1

There may be a solution to your file caching problem at the application level. It's a well-known problem in the JavaScript development world. The solution is usually called something like "output hashing".

The basic idea is to add a hash of the file contents to the file name so that the file is considered "new" and not found in the cache.

Angular does it at build time (see: --outputHashing).

1

you csn also use a exclude from the caching this file as i do

 location updater/serversettings.xml {
        expires -1;
        add_header 'Cache-Control' 'no-store, no-cache, 
 must-revalidate, proxy-revalidate, max-age=0';
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .