- Setup Apache and make a virtual host to listen it on other port like 8080
- Install varnish and listens it on port 80
- For the Varnish, you need to do configuration on two files like bellow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/etc/varnish/default.vcl | |
backend default { | |
.host = "127.0.0.1"; | |
.port = "8080"; | |
} | |
acl purge { | |
"localhost"; | |
} | |
sub vcl_recv { | |
if (req.request == "PURGE") { | |
if(!client.ip ~ purge) { | |
error 405 "Not allowed."; | |
} | |
purge("req.url ~ ^" req.url "$ && req.http.host == "req.http.host); | |
} | |
} | |
sub vcl_recv { | |
#Clears all caches if requesting method is post or put or delete | |
if (req.request == "POST" || req.request == "PUT" || req.request == "DELETE") { | |
purge("req.url == " req.url " && req.http.host == " req.http.host); | |
} | |
} | |
sub vcl_fetch { | |
# Make cache for all get request | |
if (req.request == "GET") { | |
unset beresp.http.Set-Cookie; | |
set beresp.cacheable = true; | |
set beresp.ttl = 60m; | |
} | |
if (req.url ~ "^/images/" || req.url ~ "^/javascripts" || req.url ~ "^/stylesheets"){ | |
set beresp.ttl = 60m; | |
} | |
} | |
/etc/default/varnish | |
DAEMON_OPTS="-a :80 \ | |
-T localhost:6082 \ | |
-f /etc/varnish/default.vcl \ | |
-S /etc/varnish/secret \ | |
-s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G" |
Here is the requesting flow: Browser ---> Varnish---->Apache2----->Application Server
That means, when u request through browser, it request to Varnish(on port 80) and if varnish has cached then it served directly to client(browser) without interfering apache otherwise Varnish passes the request to apache...In this way you can scale your Rails app heavily and serve a lot of requests concurrently with low memory and low resource consumed.
Some varnish useful commands:
That means, when u request through browser, it request to Varnish(on port 80) and if varnish has cached then it served directly to client(browser) without interfering apache otherwise Varnish passes the request to apache...In this way you can scale your Rails app heavily and serve a lot of requests concurrently with low memory and low resource consumed.
Some varnish useful commands:
- Restart Varnish - /etc/init.d/varnish restart
- Show Varnish statistics - varnishstat
- Test to see response is coming through Varnish -
curl -I http://www.your_app_url.com