Nginx, configure caching and compression of data - accelerate download speed

Friday, 07 February 2014 00:18

Long gone are the good old days when websites were made on the bare HTML page and weighing several tens of kilobytes. And the Internet was on dial-up.

Pages of modern websites weigh several hundred kilobytes, and often several megabytes. Images, scripts, stylesheets CSS - everything is indispensable attributes of a normal modern site without them in any way. And all this weighs "happiness" is not enough, and every time visitors coming to your site, download it all to your computer. And until the download is complete, the browser page opens.

Consider the method of dealing with this problem by means of a web server Nginx. The bottom line is that we will do 2 things - sozhmёm all static files (scripts, style files) by gzip, and zakeshiruem them with images in the browser cache visitor to every time they are not downloaded from the site and were taken directly from the cache on your computer visitor.

All settings and commands followed by a server with Centos, but configuring servers to other systems is not much different.

Setting data compression by Nginx.

Open the configuration file Nginx, located at /etc/nginx/nginx.conf

And in section http {...}, which is in the beginning of the file add what is not there from the example below:

gzip on;
gzip_static on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_proxied any;
gzip_types text/plain application/xml application/x-javascript text/javascript text/css text/json;

Configuring caching static files in the browser cache user by Nginx.

In the same file /etc/nginx/nginx.conf down below, find the right design for the site server and appends it:

location ~* ^.+.(jpg|jpeg|gif|png|ico|css|pdf|ppt|txt|bmp|rtf|js)$ {
root /var/www/user/data/www/site.ru;
expires 7d;

where expires 7d — is the number of days as the cache static files should be stored on the user's computer. If you do not make changes to css, js, files of your site and do not change the picture, it makes sense to increase this parameter up to several months or even up to a year.

For illustrative purposes, the site server section of the server that was installed Nginx standard tools panel ISPmanager:

server {
server_name site.ru www.site.ru;
listen 111.121.152.21;
disable_symlinks if_not_owner from=$root_path;
set $root_path /var/www/user/data/www/site.ru;
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
root $root_path;
access_log /var/www/nginx-logs/user isp;
access_log /var/www/httpd-logs/site.ru.access.log ;
error_page 404 = @fallback;
}

And now we add here expires 7d , now it looks like this:

server {
server_name site.ru www.site.ru;
listen 111.121.152.21;
disable_symlinks if_not_owner from=$root_path;
set $root_path /var/www/user/data/www/site.ru;
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
root $root_path;
expires 7d;
access_log /var/www/nginx-logs/user isp;
access_log /var/www/httpd-logs/site.ru.access.log ;
error_page 404 = @fallback;
}

Restart Nginx command:

service nginx restart

Go to their website and enjoy, it has now become a loaded several times faster. And the weight of pages fell from a few megabytes to a few dozen kilobytes!

This article will be useful for owners of virtual and dedicated servers as virtual hosting users do not have access to edit the configuration of Nginx. However, customers virtual hosting , proposed by our organization can always contact our support and our system administrators will make necessary settings for your site.


Leave a comment

  • Payment
    Methods:
  • visa
  • mastercard
  • qiwi
  • webmoney
  • yandex money
  • sberbank
  • mts bank
  • zpayment
  • liqpay
  • alfabank white
Copyright © 2012 - 2021 WebPatron Ltd. All rights reserved.