禁用JavaScript文件的nginx缓存 [英] Disable nginx cache for JavaScript files

查看:91
本文介绍了禁用JavaScript文件的nginx缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok, I'm almost giving up on this, but how can I disable the caching from Nginx for JavaScript files? I'm using a docker container with Nginx. When I now change something in the JavaScript file, I need multiple reloads until the new file is there.

我如何知道它的Nginx而不是浏览器/ docker?

How do I know it's Nginx and not the browser/docker?

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

Browser: I used curl on the command line to simulate the request and had the same issues. Also, I'm using a CacheKiller plugin and have cache disabled in Chrome Dev Tools.

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

Docker: When I connect to the container's bash, and use cat after changing the file, I get the correct result immediately.

I将 nginx.conf 改为 sites-enabled 改为此(我在另一个stackoverflow线程中找到)

I changed my nginx.conf for the sites-enabled to this (which I found in another stackoverflow thread)

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

然而,重建容器后(并确保它在容器中 cat ),它仍然无效。这是完整的 .conf

However, after rebuilding the containers (and making sure it's in the container with cat), it still didn't work. This here is the complete .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;
}


推荐答案

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

I have the following nginx virtual host (static content) for local development work to disable all browser caching:

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

没有发送缓存标头:

$ 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

Last-Modified is总是当前时间。

Last-Modified is always current time.

这篇关于禁用JavaScript文件的nginx缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆