How to improve Nginx performance

Gzip change the file like below /etc/nginx/nginx.conf


http {

    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 32 16k;
    gzip_http_version 1.1;
    gzip_min_length 250;
    gzip_types image/jpeg image/bmp image/svg+xml text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon;

}

If you’re installed, bortli then, you can add the below lines

load_module modules/ngx_http_brotli_static_module.so;
load_module modules/ngx_http_brotli_filter_module.so;
http {
    brotli on;
    brotli_comp_level 4;
    brotli_buffers 32 8k;
    brotli_min_length 100;
    brotli_static on;
    brotli_types image/jpeg image/bmp image/svg+xml text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon;
}

Improving server security, this is depending on your application remove # tag from the below lines /etc/nginx/nginx.conf

http {
   # add_header X-Frame-Options "SAMEORIGIN" always;
   # add_header X-XSS-Protection "1; mode=block" always;
   # add_header X-Content-Type-Options "nosniff" always;
   # add_header Referrer-Policy "no-referrer-when-downgrade" always;
   # add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
   # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

}

Optimize SSL & Session /etc/nginx/nginx.conf

http {

   ssl_stapling on;
   ssl_stapling_verify on;
   resolver 8.8.8.8 8.8.4.4 valid=60s;
   resolver_timeout 2s;

}

Improve performance by using http2 like below /etc/nginx/sites-available/your-domain.com

server{

    listen 443 http2;
    listen [::]:443 http2;
    server_name your-domain.com;

}

Browser side max cache Images, CSS, JS files /etc/nginx/sites-available/your-domain.com

server {
    location ~* \.(jpg|jpeg|png|gif|ico)$ {
       expires 30d;
    }
    location ~* \.(css|js)$ {
       expires 7d;
    }
}

After saving the above file, then, run the below command

nginx -t
systemctl restart nginx.service

Leave a Reply