禁用JavaScript文件的nginx缓存

禁用JavaScript文件的nginx缓存,javascript,caching,nginx,Javascript,Caching,Nginx,好吧,我几乎要放弃了,但是我如何才能禁用Nginx对JavaScript文件的缓存呢?我将docker容器与Nginx一起使用。当我现在更改JavaScript文件中的某些内容时,我需要多次重新加载,直到新文件出现为止 我怎么知道它是Nginx而不是浏览器/docker 浏览器:我在命令行上使用了curl来模拟请求,但遇到了相同的问题。另外,我正在使用一个CacheKiller插件,并在Chrome开发工具中禁用了缓存 Docker:当我连接到容器的bash,并在更改文件后使用cat时,我立即得

好吧,我几乎要放弃了,但是我如何才能禁用Nginx对JavaScript文件的缓存呢?我将docker容器与Nginx一起使用。当我现在更改JavaScript文件中的某些内容时,我需要多次重新加载,直到新文件出现为止

我怎么知道它是Nginx而不是浏览器/docker

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

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

我将启用的
站点的
nginx.conf
更改为此(我在另一个stackoverflow线程中发现)

但是,在重新构建容器(并确保它位于带有
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;
}

expires
add\u 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; 
}

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

您需要的是一个简单的指令,如:

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

上述操作不会缓存()中的扩展。您可以为不同的文件类型配置不同的指令。

我有以下用于本地开发工作的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';
        if_modified_since off;
        expires off;
        etag off;
    }
}
    upstream testCom
        {
         server localhost:1338;
        }

    server
        {

            listen 80;
            server_name <your ip or domain>;
            location / {

            # proxy_cache   datacache;
            proxy_cache_key $scheme$host$request_method$request_uri;
            proxy_cache_valid       200     60m;
            proxy_cache_min_uses    1;
            proxy_cache_use_stale   updating;

            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Scheme $scheme;

            proxy_ignore_headers    Set-Cookie;

            userid          on;
            userid_name     __uid;
            userid_domain   <your ip or domain>;
            userid_path     /;
            userid_expires  max;
            userid_p3p      'policyref="/w3c/p3p.xml", CP="CUR ADM OUR NOR STA NID"';


            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_pass http://testCom;
        }
    }
未发送缓存头:

$ 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
Accept-Ranges: bytes
上次修改的
始终是当前时间


注意:nginx的
$date\u gmt
格式不符合HTTP规范()。

请记住设置
发送文件关闭或缓存标头不起作用。
我用这个剪下来的:

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

我有以下用于本地开发工作的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';
        if_modified_since off;
        expires off;
        etag off;
    }
}
    upstream testCom
        {
         server localhost:1338;
        }

    server
        {

            listen 80;
            server_name <your ip or domain>;
            location / {

            # proxy_cache   datacache;
            proxy_cache_key $scheme$host$request_method$request_uri;
            proxy_cache_valid       200     60m;
            proxy_cache_min_uses    1;
            proxy_cache_use_stale   updating;

            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Scheme $scheme;

            proxy_ignore_headers    Set-Cookie;

            userid          on;
            userid_name     __uid;
            userid_domain   <your ip or domain>;
            userid_path     /;
            userid_expires  max;
            userid_p3p      'policyref="/w3c/p3p.xml", CP="CUR ADM OUR NOR STA NID"';


            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_pass http://testCom;
        }
    }
testCom上游
{
服务器本地主机:1338;
}
服务器
{
听80;
服务器名称;
地点/{
#代理缓存数据缓存;
代理缓存密钥方案主机请求方法请求uri;
代理缓存有效20060m;
代理缓存使用1;
代理缓存使用过时更新;
代理传递头服务器;
代理设置头主机$http\U主机;
代理_重定向关闭;
代理集头X-Real-IP$remote\u addr;
proxy\u set\u header X-Forwarded-For$proxy\u add\u X\u Forwarded\u For;
代理集头X-Scheme$Scheme;
代理\u忽略\u头集Cookie;
上的用户ID;
userid\u name\u uid;
用户ID_域;
userid_path/;
userid_过期最大值;
userid_p3p'policyref=“/w3c/p3p.xml”,CP=“CUR ADM OUR NOR STA NID”;
添加\u标题上次修改的$date\u gmt;
添加_头缓存控制“无存储,无缓存,必须重新验证,代理重新验证,最大年龄=0”;
如果\u已修改\u,则自关闭;
过期;
埃塔格关;
代理通行证http://testCom;
}
}

我知道这个问题有点老了,但我建议在javascript的url中使用一些缓存哈希。这在生产和开发过程中都非常有效,因为当发生更改时,您可以拥有无限的缓存时间和intant更新

假设您有一个javascript文件/js/script.min.js, 但在引用html/php文件中,您不使用实际路径,而是:

<script src="/js/script.<?php echo md5(filemtime('/js/script.min.js')); ?>.min.js"></script>
我猜filemtime调用甚至不需要在服务器上访问磁盘,因为它应该在linux的文件缓存中。如果您有疑问或静态html文件,也可以使用固定的随机值(或增量或内容散列),该值在javascript/css预处理器完成后更新,或者让其中一个git钩子更改它


理论上,您也可以使用cachebreak作为伪参数(如/js/script.min.js?cachebreak=0123456789abcfef),但由于“”,

我可以在生产中缓存该文件,而不是在开发中。我会试试你的建议你的意思是
代理无缓存
?不幸的是,即使有
代理无缓存
我也有同样的错误:/Read我有,这就是为什么我问他是指
代理无缓存
而不是
代理无缓存
。我将它设置为0(1)以外的其他值,但它也不起作用。什么是$date_gmt,是您在脚本中声明的吗?嘿,Florian,我有一个问题,每当我部署供应商css时,应用程序都需要3-4分钟才能看到,直到它返回401。你能告诉我为什么吗?大约3-4分钟后,你不断刷新浏览器,最终它会出现。这发生在css文件中。。。。你见过这个吗?您将如何解决这个问题?您是否使用一些cdn或执行缓存管理的服务工作者?您可以尝试使用wget或curl,并使用任意随机md5值。由于nginx config将任何32个字符的a-f0-9值映射到该文件,因此它应该完全独立于部署。不管它是否在某处被引用。似乎有问题的文件是通过webpack散列创建的,是的,它们位于cdn上。您只需要
无存储
。From:虽然可以设置其他指令[除no store外],但这是在现代浏览器上防止缓存响应所需的唯一指令。已暗示最大年龄=0。设置“必须重新验证”没有意义,因为要进行重新验证,需要将响应存储在缓存中,而缓存中没有存储prevents@GregK在回复中,最后修改的:2017年7月24日星期一16:19:30 GMT是wro