Wondering how to configure Varnish with Apache? We can help you with it.
Varnish Cache (commonly known as Varnish), is an open-source, popular reverse-proxy HTTP accelerator intended for speeding up web servers.
Here at Bobcares, we have seen several such Apache-related queries as part of our Server Management Services for web hosts and online service providers.
Today, we’ll take a look at how to configure Varnish with Apache.
How to configure Varnish with Apache
Now let’s take a look at how our Support Engineers configure install and use Varnish Cache 6.5 as a front-end to an Apache web server in CentOS 7 (also works on RHEL 7).
Step 1: Install Apache Web Server on CentOS 7
1. First, we install the Apache HTTP server from the default CentOS software repositories using the YUM package manager as below.
# yum install httpd
2. After the Apache installation completes, we start it for the time being and enable it to start automatically at system boot.
# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd
3. Next, we update system firewall rules to permit inbound packets on port 80 using the below commands.
# firewall-cmd –zone=public –permanent –add-service=http
# firewall-cmd –reload
Step 2: Install Varnish Cache on CentOS 7
1. Now there are pre-compiled RPM packages for the latest version of Varnish Cache. So we need to add the official Varnish Cache repository.
Before that, we need to enable the EPEL repository to install several dependency packages.
# yum install -y epel-release
2. Next, we install pygpgme, a package for handling GPG signatures and yum-utils, a collection of useful utilities that extend yum’s native features in various ways.
# yum install pygpgme yum-utils
3. After that, we create a file named /etc/yum.repos.d/varnishcache_varnish65.repo. It contains the repository configuration.
# vi /etc/yum.repos.d/varnishcache_varnish65.repo
Also, here we ensure to replace el and 7 in the config below with our Linux distribution and version:
[varnishcache_varnish65] name=varnishcache_varnish65 baseurl=https://packagecloud.io/varnishcache/varnish65/el/7/$basearch repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/varnishcache/varnish65/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300 [varnishcache_varnish65-source] name=varnishcache_varnish65-source baseurl=https://packagecloud.io/varnishcache/varnish65/el/7/SRPMS repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/varnishcache/varnish65/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300
4. Now, we run the command below to update the local yum cache and install the varnish cache package.
# yum -q makecache -y –disablerepo=’*’ –enablerepo=’varnishcache_varnish65′
# yum install varnish
5. After installing Varnish Cache, the main executable will be installed as /usr/sbin/varnishd and varnish configuration files will be located in /etc/varnish/:
6. Now, we start the varnish service. Then we enable it so that it automatically starts during system boot and verify its status to ensure that it is up and running.
# systemctl start varnish
# systemctl enable varnish
# systemctl status varnish
7. We then can confirm that the Varnish installation was successful by seeing the location of the Varnish executable and version installed on our system.
$ which varnishd
$ varnishd -V
Step 3: Configure Apache to Work With Varnish Cache
1. Now, we configure Apache to work in conjunction with Varnish Cache. Apache listens on port 80 by default. So we need to change the default HTTPD port to 8080 – this will ensure that HTTPD runs behind Varnish caching.
We make use of the sed command to change port 80 to 8080.
# sed -i “s/Listen 80/Listen 8080/” /etc/httpd/conf/httpd.conf
Also, we need to change the port on our virtual host configuration for each website that we want to serve via Varnish. Here is the configuration for our test site (/etc/httpd/conf.d/bobcares.com.conf).
<VirtualHost *:8080> DocumentRoot “/var/www/html/bobcares.com/” ServerName www.bobcares.com # Other directives here </VirtualHost>
2. Next, we open the varnish systemd configuration file. In this, we find the parameter ExecStart which specifies the port Varnish listens on and change its value from 6081 to 80.
# systemctl edit –full varnish
Once it finishes, the configuration should look like this.
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
3. Next, we set up Apache as a backend server for Varnish proxy, in the /etc/varnish/default.vcl configuration file.
# vi /etc/varnish/default.vcl
Then we find the backend section and define the host IP address and port. Here is the default backend configuration, we set this to point to our actual content server.
backend default { .host = “127.0.0.1”; .port = “8080”; }
In case, if the backend server is running on a different server with address 10.42.1.10, then the host parameter should point to this IP address.
backend server1 { .host = “10.42.1.10”; .port = “8080”; }
4. After performing all the necessary configurations, restart HTTPD and Varnish cache to effect the above changes.
# systemctl daemon-reload
# systemctl restart httpd
# systemctl restart varnish
Step 4: Test Varnish Cache on Apache
1. Finally, we test, if Varnish is enabled and working with the HTTPD service using the below cURL command.
# curl -I http://localhost
How to fix Varnish Cache Server Error
We have received many requests from our customers to resolve the below error that occurs due to varnish cache.
error: Error 503 Service Unavailable / Guru Meditation with an XID number.
This error means that the web server trying to be reached is unavailable – this could be because it is overloaded, down for maintenance, or not fully functional for another reason. Generally, Varnish Cache issues the Guru Meditation error when a connection has timed out or the Varnish Cache server has made too many requests to the back end server without getting a response.
Now let’s see how our Support Engineers resolve this error.
First, we look into the logs for all 503 errors. In order to get varnishlog to log 503 errors, we use the below command from Varnish Cache:
$ varnishlog -q ‘RespStatus == 503’ -g request
Normally, we get 503 errors because our back end is down. In this case, varnishlog could return something like “FetchError c no backend connection”. So we must check the port Varnish Cache is trying to connect to, the origin server and HTTP services such as Apache or Nginx, and see if all of that is operating correctly – if it is not, we will need to troubleshoot the back end.
However, if the backend seems to be up and running well and still we are getting 503 varnish cache error then there is something wrong with the web server’s connection to Varnish Cache or the Varnish cache configuration.
Then we change or add a .connect_timeout = Xs and a .first_byte_timeout = Xs in the backend default VCL section to a timeout length that works for web server.
Also, we try disabling KeepAlive so that idle connections will be dropped. This would look like the below:
“origin”: { “address”: “origin.example.com”, “disable_keepalive”: true }
[Need any further assistance with Apache queries? – We are here to help you.]
Conclusion
Today, we saw how our Support Engineers configure Varnish cache on the Apache server.
0 Comments