禁用JavaScript文件的nginx缓存

Mus*_*abe 31 javascript caching nginx

好吧,我几乎放弃了这个,但是如何禁用Nginx对JavaScript文件的缓存?我正在使用Nginx的docker容器.当我现在改变JavaScript文件中的内容时,我需要多次重新加载,直到新文件存在.

我怎么知道它是Nginx而不是浏览器/码头工具?

浏览器:我curl在命令行上使用模拟请求并遇到了同样的问题.此外,我正在使用CacheKiller插件并在Chrome开发工具中禁用缓存.

Docker:当我连接到容器的bash,并cat在更改文件后使用时,我立即得到正确的结果.

我改变了我nginx.confsites-enabled这个(我在另一个线程计算器中)

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|xml|html|htm)$ {
    # clear all access_log directives for the current level
    access_log off;
    add_header Cache-Control no-cache;
    # set the Expires header to 31 December 2037 23:59:59 GMT, and the Cache-Control max-age to 10 years
    expires 1s;
}
Run Code Online (Sandbox Code Playgroud)

但是,在重建容器(并确保它在容器中cat)后,它仍然无法正常工作.这是完整的.conf

server {
    server_name app;
    root /var/www/app/web;

    # Redirect to blog
    location ~* ^/blog {
        proxy_set_header Accept-Encoding "";
        sub_filter 'https://testproject.wordpress.com/' '/blog/';
        sub_filter_once off;
        rewrite ^/blog/(.*) /$1 break;
        rewrite ^/blog / break;
        proxy_pass     https://testproject.wordpress.com;
    }

    # Serve index.html only for exact root URL
    location / {
        try_files $uri /app_dev.php$is_args$args;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app_dev.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|xml|html|htm)$ {
        # clear all access_log directives for the current level
        access_log off;
        add_header Cache-Control no-cache;
        # set the Expires header to 31 December 2037 23:59:59 GMT, and the Cache-Control max-age to 10 years
        expires 1s;
    }


    error_log /var/log/nginx/app_error.log;
    access_log /var/log/nginx/app_access.log;
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*g K 62

我有以下nginx虚拟主机(静态内容)用于本地开发工作以禁用所有浏览器缓存:

server {
    listen 8080;
    server_name localhost;

    location / {
        root /your/site/public;
        index index.html;

        # kill cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
    }
}
Run Code Online (Sandbox Code Playgroud)

没有发送缓存标头:

$ curl -I http://localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 24 Jul 2017 16:19:30 GMT
Content-Type: text/html
Content-Length: 2076
Connection: keep-alive
Last-Modified: Monday, 24-Jul-2017 16:19:30 GMT
Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0
Accept-Ranges: bytes
Run Code Online (Sandbox Code Playgroud)

Last-Modified 总是当前的时间.

  • 你只需要`no-store`。来自[MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control):虽然可以设置其他指令[除了 no-store],但仅此一个您需要防止现代浏览器上的缓存响应的指令。max-age=0 已经暗示了。设置 Must-revalidate 没有意义,因为为了进行重新验证,您需要将响应存储在缓存中,而 no-store 会阻止这种情况 (7认同)

Jos*_*ald 19

expiresadd_header指令对NGINX缓存文件没有影响,这些都是纯粹的关于浏览器看到的内容.

你可能想要的是:

location stuffyoudontwanttocache {
    # don't cache it
    proxy_no_cache 1;
    # even if cached, don't try to use it
    proxy_cache_bypass 1; 
}
Run Code Online (Sandbox Code Playgroud)

虽然通常.js等是你要缓存的东西,所以也许你应该完全禁用缓存?


Nit*_*tai 9

您正在寻找的是一个简单的指令,如:

location ~* \.(?:manifest|appcache|html?|xml|json)$ {
    expires -1;
}
Run Code Online (Sandbox Code Playgroud)

以上内容不会缓存()中的扩展名.您可以为不同的文件类型配置不同的指令.


小智 7

请记住设置sendfile off;或缓存标头不起作用。我用这个剪断了:

location / {

        index index.php index.html index.htm;
        try_files $uri $uri/ =404; #.s. el /index.html para html5Mode de angular

        #.s. kill cache. use in dev
        sendfile off;
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
        proxy_no_cache 1;
        proxy_cache_bypass 1; 
    }
Run Code Online (Sandbox Code Playgroud)