好的,我对此几乎已经放弃了,但我该如何禁用Nginx对JavaScript文件的缓存呢?我正在使用带有Nginx的docker容器。当我改变JavaScript文件中的内容时,我需要多次重新加载才能看到新文件。
我怎么知道是Nginx而不是浏览器/docker的问题呢?
浏览器:我在命令行上使用curl
模拟请求时也遇到了同样的问题。此外,我正在使用一个CacheKiller
插件,并且在Chrome Dev Tools中禁用了缓存。
Docker:当我连接到容器的bash并在更改文件后使用cat
时,我立即得到了正确的结果。
我将sites-enabled
下的nginx.conf
更改为以下内容(我在另一个stackoverflow线程中找到):
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
文件。
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;
}
no-store
。来自MDN:虽然除了no-store
之外还可以设置其他指令,但在现代浏览器中,只需使用这个指令就足以防止缓存响应。max-age=0
已经被隐含地包含在其中。设置must-revalidate
没有意义,因为为了进行重新验证,需要将响应存储在缓存中,而no-store
可以防止这种情况发生。 - felixbade$date_gmt
格式与HTTP头规范不匹配。看起来解决方案包括一个服务器端包含模块。 - Greg K