Configure Varnish +Nginx +Apache
Configure Varnish +Nginx +Apache
How to improve the performance of the existing apache webserver?
We can enhance/improve LAMP server functionality by running both Varnish and Nginx along with Apache webserver.
The main thing to be noted is that we are caching responses from Apache webserver in a way that every request can be retrieved from the cache itself.Further performance can be improved by making use of nginx, which can handle more connections by keeping server load in check.
Our Goal:
Our Goal is to install nginx webserver in front end, apache at the bottom end and varnish between them for running PHP pages and video pages.
I have setup webserver with Nginx Listening on port 80 and setup Varnish to run on port 8080. Also, the HTTPD running on port 8081 together with them.
Goal can be comparison or benchmark inferences from apache standalone, apache + nginx and apache+nginx+varnish
Installation of Nginx:
Installation steps are not required as there are as much artcicles ont eh same. So our focus is on analysing the benchmark test and a comparison result.
Nginx is a high performance webserver which is more flexible than the apache webserver. We can configure nginx by following the steps given below:
# yum install epel-release # yum install nginx # systemctl start nginx # systemctl enable nginx
Installation of Varnish:
Varnish is an apache accelerator which is useful tool for speeding up apache webserver. It is a front-end caching software used in front of Apache webserver to increase server speed/responsiveness.
It can increase website perfomance and prevent the server from over load.
For installing Varnish follow the steps given below:
# yum install varnish
# systemctl start varnish
# systemctl enable varnish
# varnish -v
Installation of apache:
Apache web server is one of the most widley used webserver and it is the most flexible too. Follow the steps given below for installing apache webserver:
# yum install httpd
# service httpd start
Installation of PHP:
# yum install php
# yum install php-mysql php-pdo php-soap php-mbstring phpmyadmin
How to configure the nginx:
Add the following configuration to use the nginx on front end of apache.
cat /etc/nginx/sites-enabled/testing.com.conf
server {
listen 80;
server_name newtesting.com;
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www;
index index.php index.html;
expires 30d;
}
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Forwarded-Port 80;
proxy_set_header Host $host;
}
}
How to configure varnish:
Since nginx is running on port 80, we need to modify varnish to work on port 8080. Only then Varnish responds directly to apache in the backend port 8081. For that, modify Varnish system configuration as follows:
cat /etc/varnish/default.vcl
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = “192.168.1.218”;
.port = “8081”;
.connect_timeout = 300s; # Wait a maximum of 1s for backend connection
.first_byte_timeout = 200s; # Wait a maximum of 5s for the first byte from your backend
.between_bytes_timeout = 300s; # Wait a maximum of 2s between each bytes sent
}
How to configure Apache:
A sample virtual host for apache is as follows:
cat /etc/httpd/httpd.conf
<VirtualHost *:8081>
ServerName newtesting.com
DocumentRoot /var/www
<Directory /var/www/>
Options Indexes FollowSymLinks
</Directory>
</VirtualHost>
Now we have configured nginx, varnish, apache on ports 80, 8080,8081 respectively.
We can verify the Listening ports by:
# netstat -plant | grep LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3428/nginx
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 3291/varnishd
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 3456/httpd
tcp 0 0 :::8080 :::* LISTEN 3291/varnishd
To check the usage of all the varnish and nginx(using the curl command):
[[email protected] www]# curl -I newtesting.com
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Tue, 14 Feb 2017 10:03:12 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 9
Connection: keep-alive
Last-Modified: Thu, 03 Nov 2016 18:04:07 GMT
ETag: “2101f-9-5406962368260”
X-Varnish: 10
Age: 0
Via: 1.1 varnish-v4
Benchmark result:
With NginX+Varnish+Apache
===============================
Concurrency Level: 1000
Time taken for tests: 1.745 seconds
Complete requests: 10000
Failed requests: 0
Time per request: 174.482 [ms] (mean)
Time per request: 0.174 [ms] (mean, across all concurrent requests)
With Apache-only
===============================
Concurrency Level: 1000
Time taken for tests: 3.046 seconds
Complete requests: 10000
Failed requests: 0
Time per request: 304.622 [ms] (mean)
Time per request: 0.305 [ms] (mean, across all concurrent requests)
Conclusion:
Based on comparison results, make the conclusion.
From this we can conclude that the setup involving Nginx+Varnish performed much better than the standalone Apache Webserver.