【技术心得】Last-Modified,Etag,Expire区别

本文详细解释了HTTP头部中Last-Modified、Etag及Expire的作用与区别,包括它们如何帮助浏览器进行缓存控制,减少不必要的HTTP请求,提高网页加载速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【技术心得】Last-Modified,Etag,Expire 区别

Last-Modified 是什么

Last-ModifiedHttpHeader 中的资源的最后修改时间,如果带有 Last-Modified ,下一次发送 Http 请求时,将会发生带 If-modified-sinceHttpHeader 。如果没有过期,将会收到 304 的响应,从缓存中读取。

Etag 是什么

EtagHttpHeader 中代表资源的标签,在服务器端生成。如果带有 Etag ,下一次发送带 Etag 的请求,如果 Etag 没有变化将收到 304 的响应,从缓存中读取。

Etag 在使用时要注意相同资源多台 Web 服务器的 Etag 的一致性。

 

Expire 是什么

ExpireHttpHeader 中代表资源的过期时间,由服务器段设置。如果带有 Expire ,则在 Expire 过期前不会发生 Http 请求,直接从缓存中读取。用户强制 F5 例外。

Last-Modified,Etag,Expire 混合

通常 Last-Modified,Etag,Expire 是一起混合使用的,特别是 Last-ModifiedExpire 经常一起使用,因为 Expire 可以让浏览器完全不发起 Http 请求,而当浏览器强制 F5 的时候又有 Last-Modified ,这样就很好的达到了浏览器段缓存的效果。

EtagExpire 一起使用时,先判断 Expire ,如果已经过期,再发起 Http 请求,如果 Etag 也过期,则返回 200 响应。如果 Etag 没有过期则返回 304 响应。

Last-Modified,Etag,Expires 三个同时使用时。先判断 Expire ,然后发送 Http 请求,服务器先判断 last-modified ,再判断 Etag ,必须都没有过期,才能返回 304 响应。

 

按照上面定义,请帮我标注出来[root@keepmoving ~]# curl -v -H "Origin: http://example.com" -X GET "https://www.keepmoving123.cn/test/test123.html" Note: Unnecessary use of -X or --request, GET is already inferred. * Trying 203.193.226.100:443... * Connected to www.keepmoving123.cn (203.193.226.100) port 443 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * successfully set certificate verify locations: * CAfile: /etc/pki/tls/certs/ca-bundle.crt * CApath: none * TLSv1.3 (OUT), TLS handshake, Client hello (1): * TLSv1.3 (IN), TLS handshake, Server hello (2): * TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8): * TLSv1.3 (IN), TLS handshake, Certificate (11): * TLSv1.3 (IN), TLS handshake, CERT verify (15): * TLSv1.3 (IN), TLS handshake, Finished (20): * TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1): * TLSv1.3 (OUT), TLS handshake, Finished (20): * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 * ALPN, server accepted to use h2 * Server certificate: * subject: CN=keepmoving123.cn * start date: Feb 8 00:00:00 2025 GMT * expire date: May 8 23:59:59 2025 GMT * subjectAltName: host "www.keepmoving123.cn" matched cert's "www.keepmoving123.cn" * issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=Encryption Everywhere DV TLS CA - G2 * SSL certificate verify ok. * Using HTTP2, server supports multiplexing * Connection state changed (HTTP/2 confirmed) * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0 * Using Stream ID: 1 (easy handle 0x55b69735fd90) > GET /test/test123.html HTTP/2 > Host: www.keepmoving123.cn > user-agent: curl/7.79.1 > accept: */* > origin: http://example.com > * TLSv1.3 (IN), TLS handshake, Newsession Ticket (4): * TLSv1.3 (IN), TLS handshake, Newsession Ticket (4): * old SSL session ID is stale, removing * Connection state changed (MAX_CONCURRENT_STREAMS == 128)! < HTTP/2 200 < date: Thu, 13 Mar 2025 09:34:15 GMT < content-type: text/html < content-length: 370 < server: openresty < last-modified: Thu, 06 Mar 2025 13:37:33 GMT < etag: "67c9a51d-172" < x-ccdn-expires: 2591893 < via: CHN-GZguiyang-AREACT1-CACHE58[29],CHN-GZguiyang-AREACT1-CACHE19[0,TCP_HIT,20],CHN-HNchangsha-GLOBAL1-CACHE34[43],CHN-HNchangsha-GLOBAL1-CACHE19[0,TCP_HIT,36] < x-hcs-proxy-type: 1 < x-ccdn-cachettl: 2592000 < x-ccdn-req-id-46b1: e47b7ecf9522344085fd872944737d22 < nginx-hit: 1 < age: 107 < accept-ranges: bytes < access-control-expose-headers: Content-Length,Content-Encoding < access-control-allow-origin: http://*.exampel.com < access-control-allow-methods: GET,POST,HEAD,OPTIONS < content-disposition: attachment;filename=test150x150.jpg < access-control-allow-headers: X- Custom-Header,Origin, X-Requested-With, Content-Type, Accept, Authorization < strict-transport-security: max-age=36000 < <!DOCTYPE html> <html lang="en"> <head> <meta name="viewport" content="width=device-width" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>keepmoving test</title> </head> <body> <h1>KeepMoving</h1> <h2>CDN启蒙实践</h2> <h2>卜永进 test</h2> <h2>buyongjin test</h2> <h2>buyongjin test</h2> <h2>buyongjin test</h2> </body> </html> * Connection #0 to host www.keepmoving123.cn left intact
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值