squid判断文件是否修改机制分析

前提:

1.我写了一个简单的http服务器,以下简称 httpserver

2.前端使用squid做反向代理,以下简称 squid。squid同时反向代理了2台http服务器,其中一台是httpserver,另外一台是nginx

问题:

查看access.log发现,通过squid请求httpserver上的文件时,squid的都会TCP_MISS。

但是通过同一个squid,nginx的文件可以TCP_HT,说明squid本身应该没有问题,也许是httpserver有问题。

没有头绪,抓包分析一下吧

第一次抓包(squid与httpserver之间的包):

squid向httpserver发起GET,httpserver返回了信息,httpcode 200.没有发现什么问题。

第二次抓包(squid与nginx之间的包):

squid向nginx发起了GET,nginx不是返回200,而是返回304 not modified.

查看http header,发现squid发GET请求时,http header中有一项:If-Modified-Since: Sat, 15 Feb 2014 08:36:33

nginx回复squid时,http header中有一项:Last-Modified: Sat, 15 Feb 2014 08:36:33 GMT

似乎发现了什么。。

对话大致是这样的:

squid问nginx:从15 Feb 2014 08:36:33这个时间开始,这个文件有没有被修改过?

nginx答squid:上次修改时间是15 Feb 2014 08:36:33

也就是说这个文件没有修改,这就完成了一次缓存确认的过程。

第三次抓包(抓squid与nginx之间,从未被缓存过的文件):

squid向nginx发起了GET,nginx返回200,http header中有一项为 Cache-Control: max-age=25920

这个http header说明这个文件是可缓存的,缓存时间为25920。

那么httpserver要这么改:

1.在发送文件的http header中加上Cache-Control

2.处理请求中的http header的If-Modified-Since项。

Last-Modified格式代码如下:

随手写的,格式注意改改

1
2
3
4
5
//Last-Modified: Sat, 15 Feb 2014 08:36:33 GMT
time_t tNowTime = time(NULL);
tm* tTm= localtime(&tNowTime);
char achbuff[100] = {0};
strftime(achbuff, 100, "Last-Modified: %a, %d %b %Y %H:%M:%S GMT ", tTm);
原文地址:https://www.cnblogs.com/solohac/p/4154156.html