Setup Varnish on Port 80

Sometimes you need to quickly setup Varnish, usually in an emergency (like, your website got featured on Reddit’s frontpage 😀 ), to quickly absorb most of the hits hitting your website.

But the webserver is already using port 80.
Now what ?

Pretty easy actually :

  1. Setup Varnish on other port, say, 6081
  2. Run an iptables command : to forward incoming traffic from port 80 to port 6081
  3. Make sure Varnish uses as the backend

Presto – now all the traffic hits Varnish first – which will process them in lightning speed.

Alright, so here’s the gory detail, also available on :

Enjoy !


apt-get update ; apt-get -y install varnish

# Varnish should be already configured to list on port 6081
# if in doubt, check /etc/default/varnish,
# and look for the following line :
# DAEMON_OPTS="-a :6081

# edit varnish config
vi /etc/varnish/default.vcl

# make sure the .port line is set to 80, like this :
# .port = "80";
# then save & exit

# enable Apache's expires & headers module
a2enmod expires
a2enmod headers

# setup caching for static files
# via .htaccess file
echo "Header unset ETag" >> /var/www/.htaccess
echo "FileETag None" >> /var/www/.htaccess
echo "<ifmodule mod_expires.c>" >> /var/www/.htaccess
echo "<filesmatch \"(?i)^.*\\.(ico|flv|jpg|jpeg|png|gif|js|css)$\">" >> /var/www/.htaccess
echo "ExpiresActive On" >> /var/www/.htaccess
echo "ExpiresDefault \"access plus 2 minute\"" >> /var/www/.htaccess
echo "</filesmatch>" >> /var/www/.htaccess
echo "</ifmodule>" >> /var/www/.htaccess

# enable caching in php.ini
vi /etc/php/7.0/apache2/php.ini

# make sure session.cache_limiter = public
# save & exit

# restart Apache
/etc/init.d/apache2 restart

###### now let's start forwarding traffic to Varnish ######

# enable port forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
vi /etc/sysctl.conf

# add this line at the end of the file :
# net.ipv4.ip_forward = 1

# now here's the command that will actually forward the traffic from port 80 to Varnish
# change eth0 to your computer's network interface name
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 6081

# make sure this iptables setting will become permanent
apt-get -y install iptables-persistent

71 thoughts on “Setup Varnish on Port 80

  1. I beloved up to you’ll obtain performed proper here. The caricature is tasteful, your authored subject matter stylish. nevertheless, you command get bought an edginess over that you would like be delivering the following. sick indisputably come more in the past again since exactly the similar just about very often inside of case you shield this hike.

  2. Heya! I’m at work browsing your blog from my new iphone 4! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the superb work!

  3. Thank you so much for the info that you have to say, because there inforamasi which provide very useful

  4. Nice info. This is a very impressive post, Very useful information, it clarified things a lot for us. Thanks for sharing valuable tips. – Shirdi Flight packages from Chennai

  5. Dear admin, You always provide great examples and real-world applications, thank you for your valuable contributions.

  6. Howdy, i read your blog from time to time and i own a similar
    one and i was just curious if you get a lot of spam responses?
    If so how do you stop it, any plugin or anything you can advise?

    I get so much lately it’s driving me crazy so any assistance is very
    much appreciated.

  7. In order to improve your standard of living or develop in the international IT arena, I recommend that you visit this website, where you will find all the necessary information about employment and a huge number of completely different vacancies. And all this is available in just a couple of clicks, so I definitely recommend it!

  8. I think the admin of this site is in fact working hard in support of his web page, as here every stuff
    is quality based information.

  9. Hello there, I found your blog by the use
    of Google at the same time as searching for a similar subject, your website came up, it looks good.
    I’ve bookmarked it in my google bookmarks.
    Hi there, simply changed into alert to your weblog thru Google, and located that it
    is really informative. I am gonna watch out for brussels.
    I will appreciate for those who continue this in future.

    Numerous people will probably be benefited from your writing.

  10. Excellent go?ds f?om y?u, man. ? hav? have in mind yo?r
    stuff prior t? ?nd you’r? just to? g?eat. I ?ctually ?ike wh?t you have bought ?ight here,
    ce?tainly ?ike what ??u are sayin and the w?y in wh?ch during wh?ch yo? ar? ?aying it.
    Yoou ma?e it entertaining aand ?ou still tak? care of to stay it smart.
    ? c?n not wait t? rea? f?r more from you. This
    is a?tually ? tremendous website.

    My blog post; verified logo copy and paste

  11. Unquestionably believe that which you said. Your favorite justification seemed
    to be on the net the easiest thing to be aware of.
    I say to you, I definitely get irked while people consider worries that they just don’t know about.
    You managed to hit the nail upon the top and defined out the whole
    thing without having side-effects , people can take a signal.
    Will probably be back to get more. Thanks

  12. What’s ?p friends, how is ev?rything, ?nd what you w?sh f?r to say ?n t??
    topic ?f this piece of writing, in m? v?ew iits in f?ct
    remarkable in support of m?.

    my webpage: discuss

  13. I’m impressed, I have to admit. Seldom do I encounter a blog that’s both equally
    educative and amusing, and without a doubt, you’ve hit the nail on the head.
    The issue is something too few folks are speaking intelligently about.
    I am very happy I came across this in my search for something relating to this.

  14. Good post. I learn something totally new and challenging on sites I stumbleupon every day.

    It’s always interesting to read articles from other authors and
    practice a little something from other sites.

  15. I connstantly spent m? half ann houhr to re?d t??? webpage’s articles ?ll t?e
    t?me a?ong with ? mug of coffee.

    He?e i? my web page :: check ?? ?????? ????? ?? ?????? ???? (Merle)

  16. If yo? desire to improve y?ur familiarity ?nly keep visiting t??s website
    and ?e updated w?t? th? mo?t rec?nt news posted ?ere.

    A?so visit mmy website: Source

  17. Hey! I know this iis sort ?f off-topic but I nee?ed to ask.

    Does building a well-established blog ?uch
    a? ??urs require ? massive am?unt work? Iam c?mpletely new to operating a
    blog ?owever I do write in my diaqry daily. ?’d li?e to start
    ? blog ?o I can easily share my own experience and
    felings online. Plea?e let m? know if yo? have any suggestions or tips for ne? aspiring
    blog owners. Thankyou!

    m? website: weblink

  18. Thank you for any other great article. Where else could
    anybody get that kind of info in such a perfect method of writing?

    I’ve a presentation next week, and I’m on the
    look for such info.

  19. Amazing blog! ?o you have anny tips ?nd
    hints for aspiring writers? ?’m hoping to start m? own blog son but
    I’m a ?ittle lost ?n ever?thing. Would you propose starting
    ?ith a free platform ?ike WordPress or go for a
    paid option? The?e are so many choices out t?ere thnat I’m complet?ly overwhelmed ..
    Any ideas? App?eciate it!

    ?ere ?s my blog post … ?????

  20. ?ust wish too say ylur rticle ?s as astonishing.
    ?he clarity ?n you? postt i? simply excellent ?nd i can assume yyou ?re an expert
    ?n t?is subject. Well ?ith your permission all?w me
    to grab ?ou? feesd to ke?p up to ?ate ?ith forthcoming post.
    ?hanks ? million and pl?ase carry on t?? rewarding work.

    Take a ?ook att m? pag? read here

Leave a Reply

Your email address will not be published. Required fields are marked *