何时在 Nginx 中使用或不使用 sendfile on/off?

for*_*own 18 nginx cache

我们在nginx.conf很长一段时间内都有这个设置。

sendfile on;
Run Code Online (Sandbox Code Playgroud)

当我们更新了一个文件,例如/js/main.js从浏览器https://test.com/js/main.js?newrandomtimestamp访问时,它仍然会加载旧版本,除非我们从浏览器中完全刷新(清除缓存)。

但是当我们从 sendfile 更改设置时;发送文件关闭;浏览器将加载更新文件的正确版本。

对于我们的生产网络服务器,我们应该使用 sendfile 吗?或发送文件关闭;?如果发送文件打开;是必需的(可能是为了更好的缓存?更快的性能?) 那么如何解决上面提到的问题?

以下是nginx.conf我们生产服务器中的,我们使用的是 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;
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*ean 1

在应用程序级别可能有解决文件缓存问题的方法。这是JavaScript 开发界的一个众所周知的问题。该解决方案通常称为“输出哈希”。

基本思想是将文件内容的哈希值添加到文件名中,以便该文件被视为“新”文件并且在缓存中找不到。

Angular 在构建时执行此操作(请参阅:)--outputHashing