如何强制 NGINX 加载新的静态文件?

如何强制 NGINX 加载新的静态文件?

我最近向网站推送了重大更新,但遇到了一个问题,有些人由于浏览器正在加载而无法登录老的javascript 文件。我所做的一些事情包括:

  • 清除所有 JavaScript 文件的缓存
  • sendfile off在 nginx.conf 中设置
  • expires 1s在 mysite.conf 中设置
  • 明确设置 Cache-Control 标头:add_header Cache-Control no-cache;

下面是我的 nginx 配置文件。如能得到任何帮助我将不胜感激。

/etc/nginx/sites-enabled/mysite.conf

proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;

server {
    listen 80;
    server_name mysite.com;
    return 301 https://www.mysite.com$request_uri;
}

server {

        # listen for connections on all hostname/IP and at TCP port 80
        listen *:80;

        # name-based virtual hosting
        server_name www.mysite.com;

        # location of the web root for all static files (this should be changed for local development)
        root /var/mysite.com/static;

        # redirect http requests to https
        if ($http_x_forwarded_proto = "http") {
            rewrite  ^/(.*)$  https://www.mysite.com/$1 permanent;
        }

        # error pages
        error_page 403 /errors/403.html;
        error_page 404 /errors/404.html;
        error_page 408 /errors/408.html;
        error_page 500 502 503 504 /errors/500.html;  

        # error and access out
        error_log /var/log/nginx/error.mysite.log;
        access_log /var/log/nginx/access.mysite.log;

        # use Nginx's gzip static module
        gzip_static on;
        gzip_types application/x-javascript text/css;

        location / {

            # redefine and add some request header lines which will be passed along to the node server
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header X-Forwarded-Proto $scheme;

            # set the address of the node proxied server
            proxy_pass http://127.0.0.1:9001;

            # forbid all proxy_redirect directives at this level
            proxy_redirect off;
        }

        # do a regular expression match for any files ending in the list of extensions

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

}

/etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile off;
    tcp_nopush off;
    tcp_nodelay off;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

答案1

您是否尝试过手动删除缓存中的所有内容?这通常是/var/cache/nginx

我相信add_header Cache-Control no-cache;设置后应该可以防止内容被缓存,但也许在设置之前你已经缓存了一些东西?

答案2

expires -1;位置块内部的设置实际上将完全禁用缓存。

答案3

您正在忽略读者浏览器的缓存。除非您更改对象的名称(例如,在 .js 中添加版本号),或者对象是使用 ETag 或修改日期发送的,否则浏览器可能会认为该对象的版本在几十年内仍然有效,并且永远不会咨询您的服务器。

答案4

为了禁用所有 CSS 和 JS 文件的缓存,我使用了以下代码片段:

location ~* /.(css|js)$ {
  expires -1;
}

相关内容