How do Nginx provide pre-compressed content?

Asked

Viewed 419 times

8

I’ve already enabled the module Httpgzipstaticmodule, and defined the variable

gzip_static on;

But in the response headers of a GET request does not appear

Content-Encoding    gzip
  • you sent the header Accept-Encoding: gzip, deflate?

  • yes I’m getting Accept-Encoding:gzip,deflate,sdch, I checked this using firebug in firefox and the tools for Chrome developers

  • If files are have extension .gz, the headers are right and you’ve restarted Nginx, everything should work. To help more than that, just looking at the settings files, logs and the output of the command curl -v -H "Accept-Encoding: gzip,deflate,sdch" "URL_destino", in which the -v asks for verbose exit and the -H send the header as parameter.

  • I’m not using files in . gz, just hosting png, jpg and gif images

  • 2

    The module link you added above says that it is necessary that the compressed files are in the same directory as the uncompressed ones and have extension .gz. Maybe what you’re looking for is the Httpgzipmodule which compacts at the time of the request. However, I believe that it makes no sense to add more this compression in images, since this type of file already has its own compression.

2 answers

8


Look, I think you confused module. Httpgzipstaticmodule serves files pre-compressed if they exist. That is, if you have image.png and image.png.gz and the customer asks image.png, the module will serve the image.png.gz in place. Who makes the compression is you.

To compact on time, use the Nginxhttpgzipmodule. Note, however, that by default it compresses only text/html. Pay attention to the parameters of this module to configure right.

Another thing: images are already pre-compressed in their vast majority (and in all the cases you mentioned). Trying to compress them will be useless. There are some tools that can help, such as Optipng (see that article with a list). Google also has tips on. But activating gzip will be useless.

  • In the end I ended up creating a compressed version of each file (.gz), as I remembered that I have other text files like . css . js . html

1

Well, I compact the files as soon as I upload the site updates and run this script that generates the *.gz that NGINX is going to get.

#! /bin/bash

FILETYPES=( "*.woff" "*.css" "*.jpg" "*.jpeg" "*.gif" "*.png" "*.js"  )
# specify a list of directories to check recursively
DIRECTORIES="/www/siteX/static/"

for currentdir in $DIRECTORIES
do
   for i in "${FILETYPES[@]}"
   do
      find $currentdir -iname "$i" -exec bash -c 'PLAINFILE={};GZIPPEDFILE={}.gz; \
         if [ -e $GZIPPEDFILE ]; \
         then   if [ `stat --printf=%Y $PLAINFILE` -gt `stat --printf=%Y $GZIPPEDFILE` ]; \
                then    echo "$GZIPPEDFILE antigo, atualizando"; \
                        gzip -9 -f -c $PLAINFILE > $GZIPPEDFILE; \
                 fi; \
         else echo "$GZIPPEDFILE estah faltando, criando..."; \
              gzip -9 -c $PLAINFILE > $GZIPPEDFILE; \
         fi' \;
  done
done

Browser other questions tagged

You are not signed in. Login or sign up in order to post.