From 087fa3ff5d3f3bdea29513c02e1765c678834a9c Mon Sep 17 00:00:00 2001 From: nisbet-hubbard <87453615+nisbet-hubbard@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:14:38 +0800 Subject: [PATCH] Documented caching of compressed content. --- .../docs/http/caching_compressed_content.xml | 80 +++++++++++++++++++ xml/en/docs/index.xml | 4 + 2 files changed, 84 insertions(+) create mode 100644 xml/en/docs/http/caching_compressed_content.xml diff --git a/xml/en/docs/http/caching_compressed_content.xml b/xml/en/docs/http/caching_compressed_content.xml new file mode 100644 index 00000000..3ceaa97e --- /dev/null +++ b/xml/en/docs/http/caching_compressed_content.xml @@ -0,0 +1,80 @@ + + +
+ + +
+ + +It has often been asked whether nginx is able to cache the +results of on-the-fly compression. +This would not only save the hassle of generating +.gz files manually or the latency and +CPU cycles required for compressing on the fly, but also +make it possible to benefit from the performance +improvements offered by +kTLS +and SSL_sendfile(), which have to be disabled when the +gzip filter is in use. + + + +As an example, a minimal configuration for an nginx server that +caches responses from a FastCGI applications such as PHP would +normally look something like this: + +gzip on; + +fastcgi_cache_path /var/cache/nginx levels=1:2 + keys_zone=WORDPRESS:100m inactive=60m; + +location / { + try_files $uri /index.php?$args; +} + +location ~ \.php$ { + try_files $uri =404; + fastcgi_pass unix:/run/php-fpm.sock; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_cache WORDPRESS; +} + +This would only cache uncompressed responses, however. +To cache gzip-compressed responses, we need to set up +a dedicated caching server using +ngx_http_proxy_module: + +map $http_accept_encoding $encoding { + default ""; + ~.*gz.* gz; +} + +server { + … + location / { + proxy_pass http://unix:/run/nginx.sock; + proxy_cache WORDPRESS; + proxy_cache_key "$scheme$request_method$host$request_uri $encoding"; + proxy_set_header Accept-Encoding "$encoding"; + proxy_set_header Host $host; + } +} + +server { + listen unix:/run/nginx.sock; + gzip on; + … + location ~ \.php$ { + try_files $uri =404; + fastcgi_pass unix:/run/php-fpm.sock; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param HTTPS on; + … + } +} + diff --git a/xml/en/docs/index.xml b/xml/en/docs/index.xml index f7d6f431..b92db33e 100644 --- a/xml/en/docs/index.xml +++ b/xml/en/docs/index.xml @@ -145,6 +145,10 @@ + + + +