在使用Docker创build的nginx服务器中禁用CSS和Javascriptcaching

我有用Docker创build的nginx服务器。 当我更改JS或CSS文件时,在浏览器强制刷新30-60秒之后出现(是,closures浏览器caching)。 如何让他们立即出现? 我的系统是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; 

和服务器configuration:

 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

使用这个configuration,仍然…没有帮助。

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

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

尝试使用.htaccess禁用caching。 这是我的直播网站的代码。 这应该工作。

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

当在VM上使用docker(VirtualBox或者更改)时,将nginx.conf属性sendfileclosures

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