Enable Nginx Compression and compress js and images :)



  • Enable Nginx Compression

    sudo nano /etc/nginx/nginx.conf
    

    Uncomment the Following lines. If you have extra CPU on your host, you can turn up the compression level up level 9. I am running level 8 on my server.

    4026ffe1-b68a-47a1-af25-b1ac87fbfba5-image.png

    Install yui-compressor

    sudo apt install yui-compressor
    

    Create Script to run using jar file.

    sudo nano /var/www/html/compress_js.sh
    

    Paste the following contents in the file. This will search for javascript files in your /var/www/html directory. Then run the command yui-compressor --type js -o against your javascript files.

    #!/bin/sh
    for file in `find . -name "*.js"`
    do
    echo "Compressing $file …"
    yui-compressor --type js -o $file $file
    done
    

    Now run the script

    sudo sh /var/www/html/compress_js.sh
    

    You should see the following output:

    f4be8ad6-a337-4c4d-ad59-a5e5c7c880cb-image.png

    Install JPEG Optim

    sudo apt install jpegotptim
    

    Create JPEG Optim Script file

    sudo nano /var/www/html/compress_jpg.sh
    

    Put the following contents in the file. This script is pretty much the same as our other script. We are searching for all jpg files then running compression on them .

    #!/bin/sh
    for file in `find . -name "*.jpg"`
    do
    echo "Compressing $file …"
    jpegoptim $file
    done
    

    Now run the script

    sudo sh /var/www/html/compress_jpg.sh
    

    It should look like this. Also, not that jpegoptim will skip any files that would be made larger by "compression." You can see this when the percentage is notated by a negative percentage. The files are skipped in that case.

    1aeda93c-5eb2-4b5e-89d4-e857f2ea0ef3-image.png