好吧,我几乎要放弃了,但是我怎样才能禁用从 Nginx 缓存 JavaScript 文件呢?我用的是 Nginx 的码头集装箱。当我现在更改 JavaScript 文件中的某些内容时,我需要多次重新加载,直到新文件出现。
我怎么知道它是 Nginx 而不是浏览器/插件?
Browser: 我在命令行上使用 curl
来模拟请求,遇到了相同的问题。此外,我使用的是 CacheKiller
插件,并在 Chrome 开发工具中禁用了缓存。
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;
}