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