所以我的同事为我们设置了一个新奇的缓存代理系统,然后立即去度假。现在我收到来自我们的开发人员/设计人员的抱怨,说许多静态资源被缓存的时间比我见过的任何配置都长得多。
例如,某个徽标文件在 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/*;
}
您的后端响应
原始答案也是如此,这会覆盖
proxy_cache_valid
设置:所以所有的 nginx 都是 - 它使用对象的缓存副本运行,因为你的后端说它是有效的。
must-revalidate
当缓存条目被认为有效时,proxy-revalidate
什么也不做。而你的是。因此,您真的应该将此投诉重新发送给开发人员。