2

我已经通过 easyapache 4 在我的 WHM 服务器上安装了 mod_brotli - html、css、js 文件等都被压缩了。

然后我在官方文档中遇到了这个 - https://httpd.apache.org/docs/2.4/mod/mod_brotli.html#precompressed

此后,我将其添加到 WHM (post_virtualhost_global.conf) 中的 Post VirtualHost 包含文件中,而不是 htaccess,因为我希望它是服务器范围的。

如何验证这是否有效并且确实提供预压缩文件?无论哪种方式我都没有找到任何可说的,我只能确认 brotli 压缩正在使用中。无论是否包含包含,CPU 负载几乎相同,因此我怀疑它可能不会为下次保存压缩文件。

这是虚拟主机包括:

<IfModule mod_headers.c>
  # Serve brotli compressed CSS and JS files if they exist
  # and the client accepts brotli.
  RewriteCond "%{HTTP:Accept-encoding}" "br"
  RewriteCond "%{REQUEST_FILENAME}\.br" "-s"
  RewriteRule "^(.*)\.(js|css)"              "$1\.$2\.br" [QSA]

  # Serve correct content types, and prevent double compression.
  RewriteRule "\.css\.br$" "-" [T=text/css,E=no-brotli:1]
  RewriteRule "\.js\.br$"  "-" [T=text/javascript,E=no-brotli:1]


<FilesMatch "(\.js\.br|\.css\.br)$">
  # Serve correct encoding type.
  Header append Content-Encoding br

  # Force proxies to cache brotli &
  # non-brotli css/js files separately.
  Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>

这是我的 /etc/apache2/conf.2/brotli.conf

  <IfModule brotli_module>
  # Compress only a few types
  # https://httpd.apache.org/docs/trunk/mod/mod_brotli.html
  AddOutputFilterByType BROTLI_COMPRESS text/plain text/css text/html application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript

  SetOutputFilter BROTLI_COMPRESS
  SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-brotli


BrotliFilterNote Input instream
BrotliFilterNote Output outstream
BrotliFilterNote Ratio ratio

LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' brotli
CustomLog "logs/brotli_log" brotli
</IfModule>

这是/etc/apache2/conf.modules.d/115_mod_brotli.conf

# Enable mod_brotli
LoadModule brotli_module modules/mod_brotli.so

因此,如果有人可以帮助我弄清楚如何确认文件是否已预压缩,那就太好了。

编辑:我认为我的文件没有被预压缩。有人对此有任何进一步的信息吗?我在 akk 上找不到任何更多的帖子或文档

4

3 回答 3

1

I'm late to the party, but in my crash course of Brotli through Apache, the OP isn't possible.

What the Apache docs show is how to properly serve the files "if" they are pre-compressed, hence the text: "if they exist".

From what I gather in my search to understand this better, Apache can't actually pre-compress the files, this must be accomplished through a binary or extension which is out of Apache's scope.

What Apache mod_brotli does for you is dynamically compress requests on-the-fly as it's being sent. In the case of the OP, using cPanel, if you enable mod_brotli, EasyApache4 adds the necessary bits to serve and compress the files outlined in AddOutputFilterByType as Brotli. Again, these are served dynamically. Generated and served on-the-fly. As far as I can tell, these are cached in memory and not on disk.

Enabling mod_brotli is the easy way to go about enabling brotli, however it's better to pre-compress the files being served as the OP wanted due to the overhead and performance hit on having to literally compress all requests flowing through Apache. I ran across a blog where they talk about this and the difference between dynamic vs static is worth using static pre-compressed files, however if you have a small site, or maybe a really beefy hosting platform, then dynamically serving might work just fine for you.

If I'm not mistaken, you don't even need mod_brotli enabled in order to serve the pre-compressed .br files if you can figure out a way to pre-compress them.

Here's an example of using PHP to pre-compress the files: https://github.com/kjdev/php-ext-brotli

So far, no-one has answered the OP with viable ways to pre-compress files as Brotli (as I too am searching for this) but what I need to point out is that Apache doesn't do the pre-compressing and you will have to continue your search if you're looking for a static way of serving Brotli .br files.

于 2020-11-11T00:37:50.840 回答
1

要配置 Apache 以提供预压缩的 Brotli 文件:

  1. 确保 brotli 压缩文件存在于相应文件夹中的普通文件旁边。例如,如果你有一个文件 /var/www/html/index.html 也应该有 /var/www/html/index.html.br

  2. 将以下内容添加到正确的 VirtualHost 配置中:

RewriteCond %{HTTP:Accept-Encoding} br RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}.br -f RewriteRule ^(.*)$ $1.br [L]

<Files *.js.br>
  AddType "text/javascript" .br
  AddEncoding br .br
</Files>
<Files *.css.br>
  AddType "text/css" .br
  AddEncoding br .br
</Files>
<Files *.svg.br>
  AddType "image/svg+xml" .br
  AddEncoding br .br
</Files>
<Files *.html.br>
  AddType "text/html" .br
  AddEncoding br .br
</Files>

要检查是否正在提供预压缩的 brotli 文件:

您可以记录重写以查看您的重写是否有效。如果这些都在运行,那么您的预压缩 brotli 文件将被提供。在您的虚拟主机中,添加以下内容:

LogLevel alert rewrite:trace6

重新启动您的 apache2,点击您的 URL,然后 grep 以在您的 apache 错误日志中重写语句

tail -f /var/log/apache2/error.log | grep '[rewrite'
于 2018-06-20T11:58:29.743 回答
0

只需/etc/apache2/conf.2/brotli.conf暂时删除对 Apache 的引用并重新启动 Apache,您应该会看到您的预压缩 brotli 文件仍然使用 brotli 压缩交付,而动态压缩文件(例如,不存在预压缩文件的 HTML 或 CSS 或 JS)现在未压缩全部。

于 2018-04-11T15:20:14.573 回答