【问题标题】:Updates to CSS and .js files not being shown on nginx server对 CSS 和 .js 文件的更新未显示在 nginx 服务器上
【发布时间】:2016-03-21 21:28:25
【问题描述】:

我遵循了一个教程,并在 Vagrant VM 上运行了一个 nginx 服务器(使用 Chef 作为配置器)。我只有一个 html 页面,index.html,如果我对其进行更改并运行vagrant provision,当我在浏览器中转到该页面时,更改就会反映出来。但是,我也有一个 nginx 正在服务的 css 和 js 文件,如果我对它们进行更改,即使重新加载 VM 后,这些更改也不会反映在页面上。尽管我可以通过 SSH 连接到 VM 并验证文件的较新版本确实在 VM 上,但仍然如此。我无法弄清楚虚拟机是如何访问旧文件的,因为我在进行更改时覆盖了它们。也许我的 nginx 配置文件有问题?这是:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /home/vagrant/project/webroot;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

我将所有 3 个文件(js、css 和 html)文件存储在 VM 上的 webroot 中。就像我说的,我可以通过 SSH 登录并验证它们是否都在它们应该在的地方,所以我认为这一定是配置问题。我需要对 css 和 js 文件做一些特殊的事情吗?

【问题讨论】:

    标签: nginx vagrant


    【解决方案1】:

    1) 您可以将缓存控制标头添加到 Nginx 配置中。

    location ~* ^.+\.(js|css)$ {
        #old style: add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
        #old style: add_header Pragma "no-cache";
        expires -1;
        sendfile off; #if you're using virtualbox (it is not about cache, but you will need it) https://www.vagrantup.com/docs/synced-folders/virtualbox.html 
    }
    

    【讨论】:

    • 做到了!非常感谢,我真的把我的头撞到墙上了。
    • 以上表示静态文件永远不会被缓存。每次浏览器调用,都会发送静态文件。正如@Kilizo 所提到的,这个问题应该与浏览器端的缓存有关。上面的修复可能已经解决了您的问题,但它可能会让您的 nginx 服务器在每次浏览器请求时发送文件。(无缓存)。更多细节在这里。 css-tricks.com/strategies-for-cache-busting-cssstackoverflow.com/questions/30921598/…
    【解决方案2】:

    您确定您的浏览器没有缓存旧的 CSS 和 JS 文件吗?

    【讨论】:

    • 如何检查?我猜只是通过清除缓存?如果这有什么不同,我会使用 Chrome。
    • 尝试以隐身方式加载网站或先清除缓存。您可以使用开发工具检查缓存的 CSS 文件何时过期。此链接可能对devcurry.com/2012/03/… 有所帮助
    猜你喜欢
    • 2018-03-29
    • 2016-07-19
    • 2012-09-20
    • 2016-09-03
    • 2016-11-11
    • 1970-01-01
    • 2012-03-14
    • 2015-10-15
    • 1970-01-01
    相关资源
    最近更新 更多