Nginx 缓存未更新

Nginx 缓存未更新

于是我的同事为我们设置了一个新奇的缓存代理系统,然后就去度假了。现在我收到来自我们开发人员/设计师的投诉,说许多静态资源的缓存时间比我见过的任何配置规定的时间都要长得多。

例如,某个徽标文件已于 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

代理配置文件

# 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/*;
}

答案1

你的后端响应

< 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做。而您的缓存条目是有效的。所以你应该把这个投诉转发给开发人员。

相关内容