AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 549200
Accepted
jwerre
jwerre
Asked: 2013-10-29 09:54:30 +0800 CST2013-10-29 09:54:30 +0800 CST 2013-10-29 09:54:30 +0800 CST

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

  • 772

我最近向一个网站推送了一个重大更新,但我遇到了一个问题,有些人无法登录,因为他们的浏览器正在加载旧的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/*;
}
nginx
  • 7 7 个回答
  • 137489 Views

7 个回答

  • Voted
  1. Best Answer
    Abe Miessler
    2013-10-29T10:04:45+08:002013-10-29T10:04:45+08:00

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

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

    • 23
  2. sven13
    2017-04-28T05:18:14+08:002017-04-28T05:18:14+08:00

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

    • 17
  3. Gerard H. Pille
    2018-01-26T01:50:24+08:002018-01-26T01:50:24+08:00

    您忽略了读者浏览器的缓存。除非您更改对象的名称(例如,在 .js 中添加版本号),或者对象是使用 ETag 或修改日期发送的,否则浏览器可能会认为他的对象版本在几个decennia,永远不要咨询您的服务器。

    • 10
  4. Gajendra D Ambi
    2019-06-28T06:47:38+08:002019-06-28T06:47:38+08:00

    面临同样的问题。如果您使用 cloudflare 进行 DDOS 保护(如果没有,请执行)然后启用

    • 开发者模式一段时间。
    • 始终检查您的静态文件导致隐身(在谷歌浏览器中称为)窗口。
    • 停止 nginx > 删除缓存 > 启动 nginx 服务。
    • 3
  5. Peter Kowalsky
    2021-07-31T03:40:06+08:002021-07-31T03:40:06+08:00

    为了禁用所有 CSS 和 JS 文件的缓存,我使用了这个片段:

    location ~* /.(css|js)$ {
      expires -1;
    }
    
    • 2
  6. Dany
    2020-02-09T06:12:08+08:002020-02-09T06:12:08+08:00

    它与问题本身不太相关,但可能会为您节省几个小时。

    我遇到了同样的问题,在尝试了这个问题中提到的每个解决方案、答案和其他类似问题之后,我检查了我的 Cloudfare 控制面板并在那里找到了“缓存”部分。事实证明,Cloudfare 已经缓存了我的主 css 文件,并且无论返回缓存版本而不是新版本(甚至通过 wget)。

    因此,如果您使用 Cloudfare 为您的网站提供服务并遇到此问题:

    1. 转到您的控制面板,到缓存部分
    2. 按清除一切
    • 1
  7. Brunis
    2018-05-30T03:24:36+08:002018-05-30T03:24:36+08:00

    您的客户端很可能有一个缓存版本,并且他们不检查它们是否在您的服务器上被修改。所以你需要修复你的缓存设置,然后你可以将它们移动到不同的文件夹。例如。如果您将 /styles/*.css 移动到 /css/ ,并且所有从脚本到 /js/ 的 js 文件,他们的浏览器将不得不重新获取资源。

    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve