本篇文章由我的 一日一題 中的四個
Issue組合而成
不一定,由服務器中 ETag 的生成算法決定。詳見 #112
比如 nginx 中的 etag 由 last_modified 與 content_length 組成,而 last_modified 又由 mtime 組成
當編輯文件卻未更改文件內容時,mtime 也會改變,此時 etag 改變,但是文件內容沒有更改。
http 服務中靜態文件的 Last-Modified 根據什么生成
一般會選文件的 mtime,表示文件內容的修改時間
nginx 也是這樣處理的,源碼見: ngx_http_static_module.c
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = of.size;
r->headers_out.last_modified_time = of.mtime;
那為什么使用 mtime 而非 ctime
文件系統中 mtime 和 ctime 指什么,都有什么不同
在 linux 中,
mtime:modified time指文件內容改變的時間戳ctime:change time指文件屬性改變的時間戳,屬性包括mtime。而在 windows 上,它表示的是creation time
所以 ctime 會比 mtime 要大一些,使用 stat 查看文件屬性如下
$ stat hello.txt
File: ‘hello.txt’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 917526 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-12-10 16:15:55.253325208 +0800
Modify: 2019-12-10 16:15:52.740653330 +0800
Change: 2019-12-10 16:15:52.742653069 +0800
Birth: -
而 http 服務選擇 Last_Modified 時一般會選擇 mtime
