brotli gzip настройка в nginx
Использую brotli собранным в nginx
PHP 7.2.25
nginx 1.16.1
brotli 1.0.7
также использую PageSpeed собранный под nginx
config nginx
user bitrix;
worker_processes 8;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 10240;
events {
use epoll;
worker_connections 10240;
}
http {
include /etc/nginx/mime.types;
default_type application/force-download;
server_names_hash_bucket_size 128;
pagespeed on;
pagespeed FileCachePath /var/cache/nginx;
pagespeed FileCacheCleanIntervalMs -1;
pagespeed RewriteLevel CoreFilters;
pagespeed Domain https://site.ru;
pagespeed EnableFilters remove_comments,convert_png_to_jpeg,convert_jpeg_to_webp,collapse_whitespace;
pagespeed InPlaceResourceOptimization on;
pagespeed Statistics on;
pagespeed StatisticsLogging on;
pagespeed LogDir /var/log/pagespeed;
pagespeed AdminPath /pagespeed_admin;
pagespeed EnableCachePurge on;
pagespeed PurgeMethod PURGE;
pagespeed MessageBufferSize 100000;
pagespeed Disallow "*/order/*";
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types text/plain text/css text/xml application/x-javascript;
# Description of supported access log formats
log_format main '$remote_addr - $remote_user [$time_local - $upstream_response_time] '
'$status "$request" $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
log_format common '$remote_addr - - [$time_local - $upstream_response_time] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" $msec';
log_format balancer '$remote_addr - $remote_user [$time_iso8601] "$host" '
'"$request" $status $body_bytes_sent "schema: $scheme" '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for" '
'"$request_length" : "$request_time" -> $upstream_response_time';
log_format debug '$upstream_response_time,"$time_local","$remote_addr","$request",$status,$body_bytes_sent';
# upload default maps
include bx/maps/*.conf;
# settings files
include bx/settings/*.conf;
# Disable request logging in nginx by default
#access_log /var/log/nginx/access.log common;
access_log off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_max_body_size 1024m;
client_body_buffer_size 4m;
# Parameters for back-end request proxy
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 64k;
proxy_buffers 8 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 10m;
# Assign default error handlers
error_page 500 502 503 504 /500.html;
error_page 404 = /404.html;
# Content compression parameters
gzip on;
gzip_proxied any;
gzip_static on;
gzip_http_version 1.0;
gzip_types application/x-javascript application/javascript text/css;
# add_header
include bx/conf/http-add_header.conf;
# Set default website
include bx/site_enabled/*.conf;
# Set additional websites
include bx/site_ext_enabled/*.conf;
}
на кроне использую php скрипт
<?php
if(PHP_SAPI != 'cli')
{
die();
}
function scan_recursive($directory)
{
$directory = realpath($directory);
if ($d=opendir($directory))
{
while($fname=readdir($d))
{
if ($fname=='.' || $fname=='..')
{
continue;
}
else
{
$file = $directory.DIRECTORY_SEPARATOR.$fname;
if(is_file($file))
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext == 'css' or $ext == 'js')
{
$make_br = $make_gz = true;
if(file_exists($file.'.br'))
{
$o = filemtime($file);
$c = filemtime($file.'.br');
if($o > $c)
{
unlink($file.'.br');
}
else
{
$make_br = false;
}
}
if(file_exists($file.'.gz'))
{
$o = filemtime($file);
$c = filemtime($file.'.gz');
if($o > $c)
{
unlink($file.'.gz');
}
else
{
$make_gz = false;
}
}
if($make_br)
{
$cmd = 'brotli -f -k -q 11 '.$file;
echo $cmd;
echo "\n";
echo shell_exec($cmd);
echo "\n";
}
if($make_gz)
{
$cmd = 'gzip -f -c -9 '.$file.' > '.$file.'.gz';
echo $cmd;
echo "\n";
echo shell_exec($cmd);
echo "\n";
}
}
}
}
if (is_dir($directory.DIRECTORY_SEPARATOR.$fname))
{
scan_recursive($directory.DIRECTORY_SEPARATOR.$fname);
}
}
closedir($d);
}
}
echo 'Start'."\n";
scan_recursive(__DIR__.'/bitrix/cache/');
scan_recursive(__DIR__.'/bitrix/js/');
scan_recursive(__DIR__.'/bitrix/css/');
scan_recursive(__DIR__.'/bitrix/templates/');
scan_recursive('/var/cache/nginx');
echo 'Finish'."\n";
заголовки которые приходят на файл brotli, а нужен content-encoding: br
cache-control: max-age=31536000
content-encoding: gzip
content-length: 30940
content-type: application/javascript
date: Wed, 07 Oct 2020 15:45:45 GMT
etag: "5f7dbe36-78c0"
expires: Thu, 07 Oct 2021 15:45:45 GMT
server: nginx/1.16.1
status: 200
vary: Accept-Encoding
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-original-content-length: 30912
Как настроить конфиг что бы если, есть файл brotli отдать brotli, если нет brotli, но есть gzip отдать gzip, если нет и того и другого отдать обычный?
Ответы (1 шт):
PageSpeed и brotli частично не совместимы. Вот что написано в блоге:
PageSpeed module compatibility
As it stands, the PageSpeed NGINX module does not support Brotli compression internally.
You can use if together with Brotli NGINX module, but you will have to disable internal PageSpeed compression via:
pagespeed HttpCacheCompressionLevel 0;This will ensure that the Brotli compression works, at the cost of storing optimized assets uncompressed.
Таким образом, нужно отключить компрессию pagespeed через опцию:
pagespeed HttpCacheCompressionLevel 0;
В этом случае бротли будет работать.
Проверить можно таким образом, только подставьте свой адрес:
curl -isSH "accept-encoding: br" https://www.brotli.pro | grep -ai 'content-encoding'
если br - сработала компрессия бротли.
После такой доработки для accept-encoding: gzip, скорее всего, также потребуется включение компреcсии в nginx (посмотрите ещё сюда)
gzip on;
gzip_types text/plain text/css text/xml application/xml application/x-javascript application/javascript application/json;
gzip_http_version 1.1;
gzip_comp_level 8;
gzip_min_length 1500;
gzip_buffers 32 8k;
gzip_proxied any;
gzip_disable msie6;