mod_deflate 的手动替代方案
假设我没有将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种选择是将所有内容转发到 PHP 脚本,该脚本会动态压缩并缓存所有内容。对于每个请求,它都会将时间戳与缓存的版本进行比较,如果它比源文件新,则返回该版本。使用 PHP,您还可以覆盖 HTTP 标头,因此它会被正确处理,就好像它是由 Apache 本身进行 GZIP 压缩一样。
像这样的东西可能会为您完成工作:
.htaccess
cache.php:
您将需要 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
cache.php:
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.
手动和自动方法之间似乎没有很大的性能差异。我使用自动和手动压缩进行了一些 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.
您还可以使用 mod_ext_filter 并通过 gzip 进行管道传输。事实上,这是一个例子:
这样做的优点是非常非常简单……缺点是会多出一个
fork()
和exec()
对每个请求,这显然会对性能产生很小的影响。You could also use mod_ext_filter and pipe things through gzip. In fact, it's one of the examples:
The advantage of this is that it's really, really easy… The disadvantage is that there will be an additional
fork()
andexec()
on each request, which will obviously have a small impact on performance.