Apache and NginX. Performance tuning.

Updated Mar 17, 2023

Apache2 and Nginx performance tuning There are many things that affect the speed of page loading. In this post we will talk about ways to speed up the site due to the correct configuration of the web server.

Gzip compression and caching reduce the amount of data transferred from 3.3 Mb to 1.94 Mb. The compressed HTML of the main page weighs 141.71 Kb, as opposed to 939.99 Kb.
Gzip compression and caching reduce the amount of data transferred from 3.3 Mb to 1.94 Mb. The compressed HTML of the main page weighs 141.71 Kb, as opposed to 939.99 Kb.

Gzip compression

Gzip compression is the process of reducing the size of a file using special algorithms. Before transferring the file to the user, your server archives it, thereby speeding up its transfer. The browser then restores and uses the resulting file. This process gives a big gain in the speed of the site. There are different levels of compression. The higher it is, the more time it takes for the archiving procedure.

For static files such as JavaScript and CSS, you can configure the use of pre-compressed data. To do this, you will need to configure the [ngx_http_gzip_static_module] module(https://nginx.org/en/docs/http/ngx_http_gzip_static_module .html “nGinx module to send precompressed files”). The web server will search for the requested file, but with the ending .gz'. If he finds one, he will send it to the user. For example, there is a file on your server/var/www/your-site/main.js . You can create a compressed copy of it yourself/var/www/your-site/main.js.gz ` and nGinx will send it exactly.

Caching

Caching is the ability of a browser to store previously downloaded files in its memory. This way, duplicate files are downloaded only once for all pages of the site.

Caching SSL sessions

When a user’s browser opens your site over a secure connection, your server generates the time keys to create an SSL session. They can be cached to use when visiting again.

Apache Configuration

You need to enable the following modules mod_expires, mod_deflate and mod_headers. You can do this with this command:

sudo a2enmod expires;
sudo a2enmod deflate;
sudo a2enmod headers;
sudo service apache2 restart; #Restart Apache

Then add the settings to the virtual host configuration file:

<IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/javascript
 AddOutputFilterByType DEFLATE text/xml
 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE image/x-icon
 AddOutputFilterByType DEFLATE image/svg+xml
 AddOutputFilterByType DEFLATE application/json
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/x-javascript
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/x-font
 AddOutputFilterByType DEFLATE application/x-font-truetype
 AddOutputFilterByType DEFLATE application/x-font-ttf
 AddOutputFilterByType DEFLATE application/x-font-otf
 AddOutputFilterByType DEFLATE application/x-font-opentype
 AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
 AddOutputFilterByType DEFLATE font/ttf
 AddOutputFilterByType DEFLATE font/otf
 AddOutputFilterByType DEFLATE font/woff
 AddOutputFilterByType DEFLATE font/woff2
 AddOutputFilterByType DEFLATE font/opentype
</IfModule>
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType font/woff .woff
AddType font/woff2 .woff2
AddType image/svg+xml .svg
AddType image/webp .webp
<IfModule mod_expires.c>
 ExpiresActive On ExpiresDefault "access plus 5 seconds"
 ExpiresByType image/x-icon "access plus 1 week"
 ExpiresByType image/jpeg "access plus 1 week"
 ExpiresByType image/png "access plus 1 week"
 ExpiresByType image/gif "access plus 1 week"
 ExpiresByType image/webp "access plus 1 week"

 ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
 ExpiresByType font/ttf "access plus 1 year"
 ExpiresByType font/otf "access plus 1 year"
 ExpiresByType font/woff "access plus 1 year"
 ExpiresByType font/woff2 "access plus 1 year"
 ExpiresByType image/svg+xml "access plus 1 year"

 ExpiresByType application/x-shockwave-flash "access plus 1 year"
 ExpiresByType text/css "access plus 1 year"
 ExpiresByType text/javascript "access plus 1 year"
 ExpiresByType application/javascript "access plus 1 year"
 ExpiresByType application/x-javascript "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
 Header set Cache-Control "public"
</IfModule>
FileETag MTime Size

NginX Configuration

These settings must be specified in the settings of the NginX virtual host

# Enable gzip
gzip on;
gzip_comp_level 6;
gzip_types image/svg+xml text/plain text/html text/xml text/css text/javascript application/xml application/xhtml+xml application/rss+xml application/javascript application/x-javascript application/x-font-ttf application/vnd.ms-fontobject font/opentype font/ttf font/eot font/otf;
gzip_disable "MSIE [1-6]\. ";
add_header Strict-Transport-Security "max-age=31536000;
includeSubDomains" always;#Secure header
# Enable etag caching
location ~ \.(manifest|appcache|html|xml|json|rss|atom)$ {
 add_header Strict-Transport-Security "max-age=31536000;
 includeSubDomains " always; #Secure header
 expires -1;
 add_header Cache-Control "public ";
 etag on;
}
# Enable caching for images, icons, video, audio etc
location ~ \.(</code>jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp3|mp4|ogg|ogv|webm|htc|gz|tar|fla|txt|zip|rar|pdf|xml|swf|wav)$ {
 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; #Secure header
 expires 1W;
 add_header Cache-Control "public ";
 etag on;
}
# Enable caching for CSS and Javascript
location ~ \.(css|js|eot|woff2|woff|ttf|otf)$ {
 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;#Secure header
 expires 1y;
 add_header Cache-Control "public ";
 etag on;
}

If your Nginx server is running on a secure connection, it will be useful to cache SSL session data. This will save time on office operations to establish a secure connection. To do this, you need to add these settings:

ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_stapling on;

Always make a backup copy before updating configuration files.

We use cookies. By continuing to use the site, you agree to the processing of personal data in accordance with privacy policy. I agree