NGINX : browser caching and gzip compression

NGINX : browser caching and gzip compression

While serving static content from server to browser, if performance is very slow.

1. Enable GZIP compression which will serve files of specified format by zipping it between (server-browser) this minimises web serving time.  
2. Enable images caching to 6 days or more, if you want NGINX to cache images in it and serve faster. 

1. /etc/nginx/conf.d/default.conf

location ~* \favicon.ico$ {
        expires 6d;
    }

    location ~ \.css {
        root /var/www/html;
        add_header  Content-Type    text/css;
        expires 6d;
    }

    location ~* \.js {
        root /var/www/html;
        add_header  Content-Type    application/x-javascript;
        expires 6d;
    }

    location ~* \.png|jpeg|jpg {
        root /var/www/html;
        add_header  Content-Type    image/png;
        add_header  Content-Type    image/jpeg;
        add_header  Content-Type    image/jpg;
        expires 6d;
    }
    location ~* \.svg {
        root /var/www/html;
        add_header  Content-Type    image/svg+xml;
        expires 6d;
    }


2. /etc/nginx/conf.d/gzip.conf


gzip on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 256;
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/json application/xml application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_vary on;


    • Related Articles

    • Disallow Access to .git in on the webserver

      To Disallow Access to .git Place this configuration: For apache in /etc/apache2/apache.conf <DirectoryMatch "^/.*/\.git/">   Deny from all </Directorymatch> For Nginx in /etc/nginx/nginx.conf location ~ /\.git {   return 404; }
    • Nginx : How To Block Exploits, SQL Injections, File Injections, Spam, User Agents, Etc.

      server { [...] ## Block SQL injections set $block_sql_injections 0; if ($query_string ~ "union.*select.*\(") { set $block_sql_injections 1; } if ($query_string ~ "union.*all.*select.*") { set $block_sql_injections 1; } if ($query_string ~ ...
    • SSL Issue/Auto-Renew With Docker

      Issue/Renew SSL certificates using docker we will be using webroot method to get certificates with certbot  Some Examples: For Issuing New SSL docker run \ -v /etc/nginx/conf.d/ssl/certbot:/etc/letsencrypt \ -v ...
    • How: Change Upload File Limit in Docker Project

      Github > Common-Components >Select Particular branch >  orderhive-plus > templates > frontend.yaml add line :  nginx.ingress.kubernetes.io/proxy-body-size: "25m" in annotations Section
    • Steps to install Magento 2.2.6 in Kubernetes

      Step 1 : Install Magento and Create Ingress helm install --name magento-name --namespace magento-name -f /root/magento_helm/value.yaml stable/magento --version 3.3.0 Step 2 : Setup Crontab Make sure crontab is running. service cron start  Commands : ...