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 / 问题 / 660206
Accepted
Sammitch
Sammitch
Asked: 2015-01-17 15:06:16 +0800 CST2015-01-17 15:06:16 +0800 CST 2015-01-17 15:06:16 +0800 CST

Nginx 缓存不更新

  • 772

所以我的同事为我们设置了一个新奇的缓存代理系统,然后立即去度假。现在我收到来自我们的开发人员/设计人员的抱怨,说许多静态资源被缓存的时间比我见过的任何配置都长得多。

例如,某个徽标文件在 13 日已更改,但仍返回 9 日的版本,尽管设置为:proxy_cache_valid 200 1h;应该只缓存 1 小时。

据我所见,上游服务器正在向 Nginx 提供标头Expires: Sat, 14 Feb 2015 19:33:58 GMT,并且缓存到期只是在运行,而不管Last-Modified:标头已更改。我已经查看了上游服务器的日志,并且代理没有尝试检查文件的状态。

如何让 Nginx 检查更新的内容?

来自缓存的响应标头:

# curl -v -XHEAD 'http://foo.company.com/inc/skins/pt-1r/schemes/default/img/logo.png'
* About to connect() to foo.company.com port 80 (#0)
*   Trying 1.2.3.4... connected
* Connected to foo.company.com (1.2.3.4) port 80 (#0)
> HEAD /inc/skins/pt-1r/schemes/default/img/logo.png HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: foo.company.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.0.15
< Date: Thu, 15 Jan 2015 19:37:19 GMT
< Content-Type: image/png
< Connection: keep-alive
< Last-Modified: Fri, 09 Jan 2015 00:04:42 GMT
< Content-Length: 19198
< Cache-Control: max-age=2592000, public, must-revalidate, proxy-revalidate
< Expires: Thu, 12 Feb 2015 22:54:22 GMT
< Vary: User-Agent
< Content-Language: en
< X-Cache-Status: HIT
< Accept-Ranges: bytes

与直接从服务器相反:

# curl -v --header "Host: foo.company.com" -XHEAD http://10.1.2.3/inc/skins/pt-1r/schemes/default/img/logo.png
* About to connect() to 10.1.2.3 port 80 (#0)
*   Trying 10.1.2.3... connected
* Connected to 10.1.2.3 (10.1.2.3) port 80 (#0)
> HEAD /inc/skins/pt-1r/schemes/default/img/logo.png HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Accept: */*
> Host: foo.company.com
>
< HTTP/1.1 200 OK
< Date: Thu, 15 Jan 2015 19:33:58 GMT
< Server: Apache
< Last-Modified: Tue, 13 Jan 2015 23:04:44 GMT
< Accept-Ranges: bytes
< Content-Length: 45255
< Cache-Control: max-age=2592000, public, must-revalidate, proxy-revalidate
< Expires: Sat, 14 Feb 2015 19:33:58 GMT
< Vary: User-Agent
< Content-Type: image/png
< Content-Language: en

代理.conf

# Store cached date here
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=cache:128m inactive=1d max_size=1g;

# Use cache defined above
proxy_cache             cache;
proxy_cache_key         $scheme$host$request_uri;

# Only cache positive responses
proxy_cache_valid       200 1h;
proxy_cache_valid       301 302 5m;

# Temp path for when buffers overflow
proxy_temp_path /var/lib/nginx/temp;

# Buffer data (must be on to allow caching)
proxy_buffering    on;
proxy_buffer_size  128k;
proxy_buffers 100  128k;

# Set some headers
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;

# Die if backend takes too long to connect
proxy_connect_timeout   5;

# Allow adding abcnocache=1 to URLs to skip the cache
proxy_cache_bypass              $arg_abcnocache;

# Add a header showing the cache status
add_header X-Cache-Status $upstream_cache_status;

该站点的配置:

server {

    server_name foo.company.com *.foo.company.com ;
    listen 80;

    access_log    /var/log/nginx/foo.company.com-access.log;
    error_log     /var/log/nginx/foo.company.com-error.log;

    location / {
        proxy_pass  http://10.1.2.3;
        proxy_redirect default;
    }

}

nginx.conf 很好的衡量标准:

user nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
}

http {

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

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

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;

  keepalive_timeout  65;

  gzip  on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_vary off;
  gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
  gzip_min_length  1000;
  gzip_disable     "MSIE [1-6]\.";

  server_names_hash_bucket_size 64;
  types_hash_max_size 2048;
  types_hash_bucket_size 64;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}
nginx
  • 1 1 个回答
  • 3850 Views

1 个回答

  • Voted
  1. Best Answer
    drookie
    2015-01-18T07:13:19+08:002015-01-18T07:13:19+08:00

    您的后端响应

    < Cache-Control: max-age=2592000, public, must-revalidate, proxy-revalidate
    < Expires: Sat, 14 Feb 2015 19:33:58 GMT
    

    原始答案也是如此,这会覆盖proxy_cache_valid设置:

    Parameters of caching can also be set directly in the response header.
    This has higher priority than setting of caching time using the directive. 
    

    所以所有的 nginx 都是 - 它使用对象的缓存副本运行,因为你的后端说它是有效的。must-revalidate当缓存条目被认为有效时,proxy-revalidate什么也不做。而你的是。因此,您真的应该将此投诉重新发送给开发人员。

    • 1

相关问题

  • 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