Nginx – When to use or not to use sendfile on/off in Nginx

cachenginx

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;
}

Best Answer

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