Apache2 mod_expires 不起作用

Kho*_*khe 6 apache .htaccess mod-expires

我最近在支持 Apache2 的 VPS 上部署了一个网站,并且正在研究 YSlow 的性能改进建议。这些改进包括使用 mod_deflate 和 mod_expires,由于我有 root 访问权限,我可以直接编辑 Apache 配置文件。

mod_deflate 现在正在工作,但我遇到了 mod_expires 的问题,所以这是我所做的:

启用过期模块并重新启动服务器

a2enmod expires
service apache2 restart
Run Code Online (Sandbox Code Playgroud)

在 mods_enabled 下创建一个新文件“expires.conf”,其中包含以下内容:

<IfModule mod_expires.c>
  # Enable expirations
  ExpiresActive On

  # Default directive
  ExpiresDefault "access plus 1 month"

  # My favicon
  ExpiresByType image/x-icon "access plus 1 year"

  # Images
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"

  # CSS
  ExpiresByType text/css "access plus 1 month"

  # Javascript
  ExpiresByType application/javascript "access plus 1 year"
</IfModule>
Run Code Online (Sandbox Code Playgroud)

但是,当检查网络下的各种浏览器开发工具时,文件显示无缓存控制。

我想在位于我网站根目录的 .htaccess 中使用我的 mod_expires 脚本:/var/www/sitename/public_html,因为 .htaccess 中的设置会覆盖 apache 配置文件中的任何先前设置。

为此,我在 apache2.conf 中启用了 htaccess。

我通过在其中引入错误来测试我的 htaccess 文件确实有效(注释掉 IfModule 打开标记)。刷新浏览器给了我一个 500 服务器错误。因此正在处理 htaccess 文件。

不幸的是,浏览器开发工具仍然没有显示缓存控制,而且我的想法已经用完了。

我错过了什么吗?谢谢!

Kho*_*khe 0

我终于解决了这个问题:

  • 首先,我注意到所有其他配置文件和我在 mods-enabled 下创建的配置文件之间的区别:其他配置文件是指向 mods-available 下各自文件的符号链接。这不应该是一个问题,但绝对最好遵守惯例。

  • 看来我在创建文件后可能没有重新启动 apache2 服务