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 @@ + + + +