【问题标题】:Disable CSS and Javascript cache in nginx server created with Docker在使用 Docker 创建的 nginx 服务器中禁用 CSS 和 Javascript 缓存
【发布时间】:2017-06-14 22:56:41
【问题描述】:

我已经用 Docker 创建了 nginx 服务器。当我对 JS 或 CSS 文件进行更改时,它们会在 30 - 60 秒后出现,并在浏览器中强制刷新(是的,浏览器缓存已关闭)。如何让它们立即出现?我的系统是 Ubuntu 17。

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-available/*;
  open_file_cache max=100;
  client_max_body_size 4M;
}

daemon off;

以及服务器配置:

server {
    server_name l.site;
    root /var/www/site;

    index index.php;

    location / {
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        if (!-f $request_filename){
                set $rule_0 1$rule_0;
        }
        if (!-d $request_filename){
            set $rule_0 2$rule_0;
        }
        if ($request_filename !~ "-l"){
            set $rule_0 3$rule_0;
        }
        if ($rule_0 = "321"){
            rewrite ^/(.*)$ /index.php?url=$1 last;
        }
    }

    # from UPDATE #1 ->
    location ~* \.(?:css|js)$ {
      expires off;

      # don't cache it
      proxy_no_cache 1;

      # even if cached, don't try to use it
      proxy_cache_bypass 1;
    }
    # <- from UPDATE #1

    location ~ \.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;
    }

    error_log /var/log/nginx/site_error.log;
    access_log /var/log/nginx/site_access.log;
}

更新 #1

将此添加到服务器,但在代码更改后仍然没有在浏览器中显示更新的文件。

location ~* \.(?:css|js)$ {
  expires off;

  # don't cache it
  proxy_no_cache 1;

  # even if cached, don't try to use it
  proxy_cache_bypass 1;
}

更新 #2

使用THIS 配置,但仍然...没有帮助。

location / {
    add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
    expires off;
}

链接到最新版本的文件: https://gist.github.com/ktrzos/1bbf2fd0161ce0e20541ccb18fe066a5

【问题讨论】:

  • 有时候缓存也不是坏事,不如考虑加个code.js?&lt;some_rand_value&gt;
  • 我没有写缓存是一件坏事:)。我什至以相反的方式思考这个话题。我在开发环境中不需要它。 code.js 是什么东西?
  • Code.js 就是你不想被缓存的任何文件
  • 哦...我以为这是某种外部库:D。我的错。在这些问题开始时一直在尝试这种方式。也不起作用:/.
  • 如果是这样,那么它看起来与ngx设置无关。

标签: php nginx docker


【解决方案1】:

尝试使用 .htaccess 禁用缓存。这是来自我的实时网站的代码。这应该工作。

<FilesMatch "\.(html|htm|js|css|php)>
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</FilesMatch>

【讨论】:

  • 请检查我更新的问题。我需要能够立即检查我的开发服务器上项目中所做的更改,而不是等待半分钟来实现它。这非常令人沮丧。
  • @KrzysztofTrzos 我已经更新了答案。请检查
  • .htaccess 文件是否由 Apache 而不是 Nginx 使用?
  • 我有 Nginx。另外,已经用过THIS 没用……
【解决方案2】:

在虚拟机(VirtualBox 左右)上使用 docker 时,将 nginx.conf 属性 sendfile 更改为关闭

http {
  server_tokens off;
  sendfile off;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-available/*;
  open_file_cache max=100;
  client_max_body_size 4M;
}

【讨论】:

  • 是的,我在off 值上拥有该属性。 HERE你得到了我的 nginx 配置文件的最新版本。
猜你喜欢
  • 2016-06-07
  • 1970-01-01
  • 2022-08-05
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 2017-07-29
  • 1970-01-01
  • 2021-04-05
相关资源
最近更新 更多