在 Centos 上为 Apache 启用浏览器缓存

在 Centos 上为 Apache 启用浏览器缓存

我在尝试为 Centos 服务器上由 Apache 提供服务的网站启用浏览器缓存时遇到问题。

我已经完成了所有常规步骤,这些步骤以前在 Ubuntu 安装中对我有用,所以现在我有点不知所措。我一直在谷歌搜索,但还没有找到为什么它们可能需要在 Centos 和 Ubuntu 上进行不同的设置的原因。值得一提的是,mod_deflate 工作正常,任何帮助都将不胜感激。

Centos 版本

rpm -q centos-release
centos-release-6-9.el6.12.3.x86_64

Apache 版本

rpm -q httpd
httpd-2.2.15-59.el6.centos.x86_64

httpd.conf 中没有 AllowOveride None

grep 'AllowOverride' httpd.conf
AllowOverride All
# AllowOverride controls what directives may be placed in .htaccess files.
AllowOverride All
#    AllowOverride FileInfo AuthConfig Limit
# for additional configuration directives.  See also the AllowOverride
AllowOverride All
AllowOverride All
AllowOverride All

网站 VHost.conf 中没有 AllowOverride None

grep 'AllowOverride' /etc/httpd/conf.d/website-in-question.com.conf
AllowOverride All 

模块已启用

apachectl -M | grep expires
expires_module (shared)
Syntax OK
apachectl -M | grep headers
headers_module (shared)
Syntax OK

.htaccess 内容:

#SERVER COMPRESSION
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/opentype

# For Olders Browsers Which Can't Handle Compression
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

## EXPIRES CACHING ##
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType image/jpg "access 1 year"
 ExpiresByType image/jpeg "access 1 year"
 ExpiresByType image/gif "access 1 year"
 ExpiresByType image/png "access 1 year"
 ExpiresByType image/svg "access 1 year"
 ExpiresByType text/css "access 1 month"
 ExpiresByType text/html "access 1 month"
 ExpiresByType application/pdf "access 1 month"
 ExpiresByType text/x-javascript "access 1 month"
 ExpiresByType application/x-shockwave-flash "access 1 month"
 ExpiresByType image/x-icon "access 1 year"
 ExpiresDefault "access 1 month"
</IfModule>

<ifModule mod_headers.c>

 Header set Connection keep-alive

 <filesMatch ".(css|jpg|jpeg|png|gif|js|ico|svg)$">
  Header set Cache-Control "max-age=60, public"
 </filesMatch>

</ifModule>

答案1

事实证明,如果我从 .htaccess 中删除以下几行,它就会起作用:

 <filesMatch ".(css|jpg|jpeg|png|gif|js|ico|svg)$">
  Header set Cache-Control "max-age=60, public"
 </filesMatch>

现在感觉自己有点像个白痴,但是嘿,至少它还在起作用!

相关内容