Introduction :
Varnish cache is a reverse proxy server that is used to accelerate our website to speed up the performance and to reduce page load time. so basically Varnish cache is a web accelerator and known for caching HTTP reverse proxy. Varnish sits in front of any server that is run on an HTTP connection. we configure it as HTTP caching to store the copies of web pages and web resources to serves it back the next time when required.
So, Varnish Cache is a proxy server that processes requests in a way that boosts speed and gets to access the website loads up to 100 times faster.
Working :
It uses caching to store data in virtual memory and when the client requests a website to get web pages or resources, the client fetches the data resources from the virtual memory of varnish directly.
It reduces the load time when your site used to load and transfer some big data resources or transmit lots of traffic. one of the key features of varnish for its flexible performance is its own configuration language called VCL(varnish configuration language). It allows us to add and modify policies to handle requests and make enhancements.
It makes your site load faster for users. If your page doesn’t load immediately, It makes the site down in the terms of popularity and stability.
Varnish can help you keep your website from being down and slow loader.
Setup :
here I'm giving some instructions to configure varnish cache as a caching HTTP reverse proxy server.
so before we start here some prerequisites are given below.
prerequisite: a website should be running on your server and served with Nginx/apache web server.
1 . Install the varnish
:
$ sudo apt-get install -y varnish
2. Change Apache Port :
we need to change the port of our apache site from port 80 to port 8080 in the /etc/apache2/ports.conf file and also all files in the /etc/apache2/sites-enabled/ folder.
3. Configure the varnish
:
(backend configuration)
backend default { .host = "192.168.1.2"; .port = "8080"; .first_byte_timeout = 60s; .connect_timeout = 300s; }
4. Configure Varnish Deamon
:
It’s very common to run this daemon on port 80 and 443 for SSL
# /etc/default/varnish
DAEMON_OPTS="-a :6081 \ -T localhost:80 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"
5. Fix Varnish startup daemon in some Ubuntu installations :
Here is a bug you might require to edit the startup service.
// change the port 6081 to port 80 in the below line
[Unit] Description=Varnish HTTP accelerator Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd [Service] Type=simple LimitNOFILE=131072 LimitMEMLOCK=infinity ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T :6082 -f /etc/varnish/default.vcl -S // change in this line /etc/varnish/secret -s malloc,256M ExecReload=/usr/share/varnish/reload-vcl ProtectSystem=full ProtectHome=true PrivateTmp=true PrivateDevices=true [Install] WantedBy=multi-user.target
6. Run the following command and Restart the services
#Sudo systemctl reload varnish.service
Look at varnish server Ports, HTML response headers and varnish logs:
$ netstat -tulpn
So our setup is complete now every request coming from user hit the varnish port 80 and varnish will responsible to provide the web resources . We can do some modifications in the backend configuration of varnish for caching i. e. purge cache(clean cache) , disable cache etc..
Conclusion:
Varnish server is a great way to speed up the performance of the sites and keep your content up-to-date in virtual memory of varnish to reduce load times. Now you can use that complex content and not worry about traffic without worrying about severely slowing down your site.