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.


    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.

    for file in `find . -name "*.js"`
    echo "Compressing $file …"
    yui-compressor --type js -o $file $file

    Now run the script

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

    You should see the following output:


    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 .

    for file in `find . -name "*.jpg"`
    echo "Compressing $file …"
    jpegoptim $file

    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.