Varnish is an HTTP optimizer designed for content-heavy dynamic web sites and APIs. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator. Varnish is focused exclusively on HTTP, unlike other proxy servers that often support FTP, SMTP and other network protocols.
In this article, you will learn that how to install and configure varnish HTTP accelerator as a reverse proxy for Apache/Http server.
Step 1: Install and configure Apache on CentOS 7
Install Apache/Http using below command
# yum install httpd -y
After installation, start and enable httpd service using below commands.
# systemctl start httpd # systemctl enable httpd
Use below command to check httpd default port, you will see that it is running on port 80.
# netstat -plntu
Now Configure Apache on port 8080
By default Apache server runs on port 80 but we will configure it to run on port 8080.
In order to do that we have to edit its configuration file “httpd.conf”
Use below command to edit httpd.conf file.
And change the default port 80 to 8080 on the “Listen” line
# vi /etc/httpd/conf/httpd.conf
Save changes and exit.
Now restart the Apache/Httpd service and run again netstat command to verify it is running on port 8080.
# systemctl restart httpd # netstat -plntu
Step 2: Install and configure Varnish
First Install EPEL Repo using the below command.
# yum install epel-release -y
Now install Varnish using the below command.
# yum install varnish -y
After installation, start and enable varnish service using below commands.
# systemctl start varnish # systemctl enable varnish
Use below command to check varnish default ports, you will see that it is running on port 6081 and 6082.
# netstat -plntu
Configure Varnish as reverse proxy for Apache
Use below command to edit varnish.params file.
And change its default port 6081 to http port 80 on the line VARNISH_LISTEN_PORT.
# vi /etc/varnish/varnish.params
save changes and exit.
Now restart the varnish service and run again netstat command to verify it is running on port 80.
# systemctl restart varnish # netstat -plntu
Congratulations, Varnish HTTP Accelerator has been installed successfully and now it acts as a reverse proxy for Apache web server.
Step 3: Configure Firewalld
In this step, we will add HTTP and HTTPS services so that our web server can run publically.
Install and enable firewalld using below commands
# yum install firewalld -y # systemctl start firewalld # systemctl enable firewalld
Use below command to add HTTP and HTTPS services in firewalld.
Then reload firewall to apply new changes.
# firewall-cmd --add-service={http,https} --permanent # firewall-cmd --reload
Step 4: Testing
Run below command to test varnish.
Note: Replace osradar.localdomain.local with your own domain.
# curl -I osradar.localdomain.local
Congratulations! Varnish HTTP accelerator has been installed successfully and running as a reverse proxy for the Apache web server.