mod_deflate 的手动替代方案

发布于 2024-08-24 12:14:32 字数 271 浏览 4 评论 0原文

假设我没有将 mod_deflate 编译到 apache 中,并且我现在不想重新编译。手动方法有哪些缺点,例如:

AddEncoding x-gzip .gz
RewriteCond %{HTTP_ACCEPT_ENCODING} gzip
RewriteRule ^/css/styles.css$ /css/styles.css.gz

(注意:我知道 RewriteCond 的具体细节需要稍微调整)

Say I don't have mod_deflate compiled into apache, and I don't feel like recompiling right now. What are the downsides to a manual approach, e.g. something like:

AddEncoding x-gzip .gz
RewriteCond %{HTTP_ACCEPT_ENCODING} gzip
RewriteRule ^/css/styles.css$ /css/styles.css.gz

(Note: I'm aware that the specifics of that RewriteCond need to be tweaked slightly)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

客…行舟 2024-08-31 12:14:32

另一种选择是将所有内容转发到 PHP 脚本,该脚本会动态压缩并缓存所有内容。对于每个请求,它都会将时间戳与缓存的版本进行比较,如果它比源文件新,则返回该版本。使用 PHP,您还可以覆盖 HTTP 标头,因此它会被正确处理,就好像它是由 Apache 本身进行 GZIP 压缩一样。

像这样的东西可能会为您完成工作:

.htaccess

RewriteEngine On
RewriteRule ^(css/styles.css)$ cache.php?file=$1 [L]

cache.php:

<?php
// Convert path to a local file path (may need to be tweaked)
cache($_GET['file']);

// Return cached or raw file (autodetect)
function cache($file)
{
  // Regenerate cache if the source file is newer
  if (!is_file($file.'.gz') or filemtime($file.'.gz') < filemtime($file)) {
    write_cache($file);
  }

  // If the client supports GZIP, send compressed data
  if (!empty($_SERVER['HTTP_ACCEPT_ENCODING']) and strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
    header('Content-Encoding: gzip');
    readfile($file.'.gz');
  } else { // Fallback to static file
    readfile($file);
  }
  exit;
}

// Saved GZIPed version of the file
function write_cache($file)
{
  copy($file, 'compress.zlib://'.$file.'.gz');
}

您将需要 apache 的写入权限才能生成缓存版本。您可以稍微修改脚本以将缓存文件存储在不同的位置。

这还没有经过广泛的测试,可能需要根据您的需要稍作修改,但这个想法已经全部存在,应该足以让您入门。

Another alternative would be to forward everything to a PHP script, which gzips and caches everything on the fly. On every request, it would compare timestamps with the cached version and return that if it's newer than the source file. With PHP, you can also overwrite the HTTP Headers, so it is treated properly as if it was GZIPed by Apache itself.

Something like this might do the job for you:

.htaccess

RewriteEngine On
RewriteRule ^(css/styles.css)$ cache.php?file=$1 [L]

cache.php:

<?php
// Convert path to a local file path (may need to be tweaked)
cache($_GET['file']);

// Return cached or raw file (autodetect)
function cache($file)
{
  // Regenerate cache if the source file is newer
  if (!is_file($file.'.gz') or filemtime($file.'.gz') < filemtime($file)) {
    write_cache($file);
  }

  // If the client supports GZIP, send compressed data
  if (!empty($_SERVER['HTTP_ACCEPT_ENCODING']) and strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
    header('Content-Encoding: gzip');
    readfile($file.'.gz');
  } else { // Fallback to static file
    readfile($file);
  }
  exit;
}

// Saved GZIPed version of the file
function write_cache($file)
{
  copy($file, 'compress.zlib://'.$file.'.gz');
}

You will need write permissions for apache to generate the cached versions. You can modify the script slightly to store cached files in a different place.

This hasn't been extensively tested and it might need to be modified slightly for your needs, but the idea is all there and should be enough to get you started.

命比纸薄 2024-08-31 12:14:32

手动和自动方法之间似乎没有很大的性能差异。我使用自动和手动压缩进行了一些 apache-bench 运行,两次的误差都在 4% 以内。

明显的缺点是您必须在部署之前手动压缩 CSS 文件。您可能想要确保的另一件事是您的配置是否正确。当我尝试手动方法时,我无法让 wget 自动解码 css,并且 ab 报告还列出了压缩数据大小,而不是像自动压缩那样列出了未压缩数据大小。

There doesn't seem to be a big performance difference between the manual and automatic approaches. I did some apache-bench runs with automatic and manual compression and both times were within 4% of each other.

The obvious downside is that you'll have to manually compress the CSS files before deploying. The other thing you might want to make very sure is that you've got the configurations right. I couldn't get wget to auto-decode the css when I tried the manual approach and ab reports also listed the compressed data size instead of uncompressed ones as with automatic compression.

被你宠の有点坏 2024-08-31 12:14:32

您还可以使用 mod_ext_filter 并通过 gzip 进行管道传输。事实上,这是一个例子:

# mod_ext_filter directive to define the external filter
ExtFilterDefine gzip mode=output cmd=/bin/gzip

<Location /gzipped>
# core directive to cause the gzip filter to be
# run on output
SetOutputFilter gzip

# mod_header directive to add
# "Content-Encoding: gzip" header field
Header set Content-Encoding gzip
</Location>

这样做的优点是非常非常简单……缺点是会多出一个 fork()exec() 对每个请求,这显然会对性能产生很小的影响。

You could also use mod_ext_filter and pipe things through gzip. In fact, it's one of the examples:

# mod_ext_filter directive to define the external filter
ExtFilterDefine gzip mode=output cmd=/bin/gzip

<Location /gzipped>
# core directive to cause the gzip filter to be
# run on output
SetOutputFilter gzip

# mod_header directive to add
# "Content-Encoding: gzip" header field
Header set Content-Encoding gzip
</Location>

The advantage of this is that it's really, really easy… The disadvantage is that there will be an additional fork() and exec() on each request, which will obviously have a small impact on performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文