缓存在apache服务器上无法正常工作?

时间:2019-08-09 07:49:03

标签: apache

我希望浏览器将js文件缓存1年,并使用缓存的版本,直到Expires标头条件匹配为止。我不希望浏览器检查较新的版本并继续缓存版本,直到Expires Header时间到。即使服务器上的文件已更改,我也不希望浏览器使用更新的文件,而是使用缓存的文件。我在htaccess文件中使用以下代码:

<FilesMatch "\.(js)">
ExpiresActive on
ExpiresDefault "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
Header set Cache-Control "max-age=31557600, public"
Header unset ETag
FileETag None
Header unset Pragma
Header unset Last-Modified
</FilesMatch>

问题:

让我们假设“ main.js”托管在example.com上。

#首次访问

现在第一次访问http://example.com/main.js 标题下方显示在浏览器中:

HTTP/1.1 200 OK
Date: Thu, 08 Aug 2019 07:26:24 GMT
Server: Apache/2.4.39 (Win64) OpenSSL/1.1.1b PHP/7.3.5
Accept-Ranges: bytes
Content-Length: 296
Cache-Control: max-age=31557600, public
Expires: Fri, 07 Aug 2020 07:26:24 GMT
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/javascript



Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1



HTTP_STATUS : 200
Transferred : Not Cached
Transferred : 600 Bytes

第二次访问(为缓存文件提供200个响应代码)

我复制了网址“ http://example.com/main.js”,并在浏览器“网址”标签中使用了粘贴并移动选项。 下面的标题生成:

// Response Headers
HTTP/1.1 200 OK
Date: Thu, 08 Aug 2019 07:26:40 GMT
Server: Apache/2.4.39 (Win64) OpenSSL/1.1.1b PHP/7.3.5
Accept-Ranges: bytes
Content-Length: 296
Cache-Control: max-age=31557600, public
Expires: Fri, 07 Aug 2020 07:26:40 GMT
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: application/javascript

// Request Headers
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1

HTTP_STATUS : 200
Transferred : Cached
Transferred : 0 Bytes

第三次访问:

当我按重新加载图标或“ http://example.com/main.js”页面上的Ctrl + R时 浏览器从服务器请求新副本,并且不使用缓存的副本。 生成的标头如下:

// Response Headers
HTTP/1.1 200 OK
Date: Thu, 08 Aug 2019 07:26:45 GMT
Server: Apache/2.4.39 (Win64) OpenSSL/1.1.1b PHP/7.3.5
Accept-Ranges: bytes
Content-Length: 296
Cache-Control: max-age=31557600, public
Expires: Fri, 07 Aug 2020 07:26:45 GMT
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/javascript

// Request Headers
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0


HTTP_STATUS : 200
Transferred : Not Cached
Transferred : 600 Bytes

我正在使用Apache 2.4.39,并且启用了mod_headers和mod_expires模块。

0 个答案:

没有答案