我已经使用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
答案 0 :(得分:0)
尝试使用.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>
答案 1 :(得分:0)
在VM上使用docker(VirtualBox左右)时,请将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;
}