GICT knowledge base
Enable Leverage Browser Caching in Nginx
Published
Enable Leverage Browser Caching in Nginx
Browser caching plays a paramount role in cache mechanism for increasing page speed. Static files like css, js, jpeg, png, etc that are used for the website can be saved on the visitor’s computer for future access. Whenever the visitor encounters a new page on your website, the above files can be accessed from the visitor’s computer instead of your provided server, which will tremendously increase the page load speed. So one of the best ways to enhance speed is to enable leverage browser cache. In this tutorial, you can implement leverage browser caching in nginx on Linux and Plesk control panel. In addition to that, we can use it in single or multiple domains as well.
To enable leverage caching for a single domain:
1) Log into Plesk > Go to Domains > example.com > Apache & nginx Settings
2) Add the following in the Additional nginx directives:
location ~* .(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
etag on;
if_modified_since exact;
add_header Pragma “public”;
add_header Cache-Control “max-age=31536000, public”;
}
To enable leverage caching for all your domains:
You should log in to the server via SSH and please follow the following steps
1) Create an additional nginx configuration file:
# touch /etc/nginx/conf.d/expires.global
2) Then paste following directives into it:
location ~* .(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
etag on;
if_modified_since exact;
add_header Pragma “public”;
add_header Cache-Control “max-age=31536000, public”;
}
3) Then next step is to run the following command in which you will create a directory for custom configuration templates:
# mkdir -p /usr/local/psa/admin/conf/templates/custom/domain/
4) After creating the directory you need to copy the default nginx template to this newly created directory. The following operation is the copying of the same:
# cp -p /usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php /usr/local/psa/admin/conf/templates/custom/domain/
5) Open the default nginx template using any text editor.
# vi /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php
6) Now add the following line in the bottom of the file:
include /etc/nginx/conf.d/expires.global;
So, the bottom of /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php file will look like:
include /etc/nginx/conf.d/expires.global;
<?php if (is_file($VAR->domain->physicalHosting->customNginxConfigFile)): ?>
include “<?php echo $VAR->domain->physicalHosting->customNginxConfigFile ?>”;
<?php endif ?>
}
Now you will have leverage browser caching for all your domains and also the newly created domain automatically
7) Run the following command, so that it will reconfigure with all the existing domains
# /usr/local/psa/admin/bin/httpdmng –reconfigure-all
If you need any further assistance please contact our support department.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
