Nginx mit Brotli-Support selbst kompilieren und installieren
Für beide Distributionen, Ubuntu und RHEL / CentOS, können Sie auch Ihren eigenen Nginx-Server mit aktivierter Brotli-Unterstützung kompilieren. Aber zuvor muss man sicherstellen, dass man alle erforderlichen Pakete für die manuelle Kompilierung und die Installation zur Verfügung hat.
RHEL und CentOS Users:
yum groupinstall 'Development Tools' -y
Ubuntu and Debian:
sudo apt install build-essential -y
Nginx Source Files downloaden
mkdir /root/temp && cd /root/temp wget http://nginx.org/download/nginx-xxxxxx.tar.gz (xxxxx durch die aktuelle Versionsnummer ersetzen) tar -xzvf nginx-xxxxx.tar.gz
Download Brotli Nginx Modul (ngx_brotli) vom offiziellen Github Repo.
cd /root/temp git clone https://github.com/google/ngx_brotli.git cd ngx_brotli git submodule update --init --recursive
Die Ausgabe:
[root@localhost:~]git submodule update --init --recursive Submodule 'deps/brotli' (https://github.com/google/brotli.git) registered for path 'deps/brotli' Cloning into 'deps/brotli'... remote: Counting objects: 4980, done. remote: Compressing objects: 100% (59/59), done. remote: Total 4980 (delta 25), reused 52 (delta 19), pack-reused 4895 Receiving objects: 100% (4980/4980), 30.07 MiB | 10.98 MiB/s, done. Resolving deltas: 100% (3121/3121), done. Submodule path 'deps/brotli': checked out '222564a95d9ab58865a096b8d9f7324ea5f2e03e' Submodule 'research/esaxx' (https://github.com/hillbig/esaxx) registered for path 'research/esaxx' Submodule 'terryfy' (https://github.com/MacPython/terryfy.git) registered for path 'terryfy' Cloning into 'research/esaxx'... remote: Counting objects: 32, done. remote: Compressing objects: 100% (20/20), done. remote: Total 32 (delta 10), reused 32 (delta 10), pack-reused 0 Unpacking objects: 100% (32/32), done. Submodule path 'deps/brotli/research/esaxx': checked out 'ca7cb332011ec37a8436487f210f396b84bd8273' Cloning into 'terryfy'... remote: Counting objects: 918, done. remote: Total 918 (delta 0), reused 0 (delta 0), pack-reused 918 Receiving objects: 100% (918/918), 165.17 KiB | 0 bytes/s, done. Resolving deltas: 100% (478/478), done. Submodule path 'deps/brotli/terryfy': checked out '8bb673f4410819df06920fdcfd24e18d235d84f7'
Die aktuelle Nginx-Konfiguration ansehen
nginx -V
Hier ein Beispiel-Output:
[root@localhost:~]nginx -V nginx version: nginx/1.13.6 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
Alles nach “configure arguments:” kopieren und am Ende dann folgendes hinzufügen:
--add-module=/root/temp/ngx_brotli
Der komplette ./configure Befehl sieht dann in etwa so aus:
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'--add-module=/root/temp/ngx_brotli
Nginx Paket installieren
make make install
Abschliessend Nginx neu starten:
service nginx restart
oder
sudo systemctl restart nginx
Brotli Kompression in Nginx
Danach noch dien Nginx -Konfiguration editieren (nginx.conf).
Die Brotli Direktiven müssen innerhalb eines http Blocks eingetragen werden.
http { ... ... brotli on; brotli_static on; brotli_types *; ... ... }
Abschliessend noch die Konfiguration testen:
nginx -t
Ausgabe dann:
[root@server.nixcp.com:~]nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Brotli Direktiven
Wir verwenden im Beispiel 3 Direktiven.
- brotli = Brotli aktivier.en
- brotli_static = Statische und pre-compressed Dateien wie beispielsweise css, js, etc mit der .br Extension am Ende eg: jquery.js.br.
- brotli_types = Hier kann man diverse MIME-Types definieren oder einfach mit einem * als Wildcard eintragen
Weitere Brotli (ngx_brotli) Direktiven
Zum Beispiel:
- brotli_comp_level = brotli Kompressions-Level zwischen 0 to 11, Default ist 6.
- brotli_window = Brotli windows size. (1k, 2k, 4k, 8k, 16k, 32k, 64k, 128k, 256k, 512k, 1m, 2m, 4m, 8m and 16m)
- brotli_buffers = Puffergröße und Anzahl, Default ist 32 4k|16 8k.
- brotli_min_length = Minimale Dateigröße, ab der komprimiert wird. Hierfür wird der Wert des Content-Length Feldes im HTTP Header ausgelesen.
Brotli testen
Die schnellste Methode wäre mit irgendeinem Web-Services, die Webserver auf Brotli-Kompression testen.
Benutzen die bitte hierfür die Suchmaschine Ihrer Wahl. (“Test Webserver Brotli”)