大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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 unl…

问题原文

原帖链接 · forestclown · CC BY-SA 4.0

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

社区高分解答

原帖链接 · Danila Vershinin · CC BY-SA 4.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-11-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。