Setting up Apache virtual hosts behind Varnish

Varnish is a very fast front end cache server. You might want to use it at the front of Apache to speed up loading of your static pages and static media, for example for your WordPress blog. You can also use Varnish backends to multiplex the requests between Plone and Apache based PHP software running on the same server using different backend directives.

However if you wish to use Apache virtual hosts with Varnish there is a trick in it.

We use the following setup

  • Varnish listens to port 80, HTTP
  • Apache listens to port 81
  • Varnish uses Apache as a backend

The related varnish.vcl is

backend backend_apache {
.host = "127.0.0.1";
.port = "81";
}

sub vcl_recv {
 ...
 elsif (req.http.host ~ "^blog.mfabrik.com(:[0-9]+)?$") {
    set req.backend = backend_apache;
 }
 ...
}

Note that the backend IP is 127.0.0.1 (localhost). By default, with Debian or Ubuntu Linux, Apache configuration does not do virtual hosting for this.

So if /etc/apache2/sites-enabled/blog.mfabrik.com looks like:

<VirtualHost *:81>

 ServerName blog.mfabrik.com
 ...
 LogFormat       combined
 TransferLog     /var/log/apache2/blog.mfabrik.com.log

 ...

 ExpiresActive On
 ExpiresByType image/gif A3600
 ExpiresByType image/png A3600
 ExpiresByType image/image/vnd.microsoft.icon A3600
 ExpiresByType image/jpeg A3600
 ExpiresByType text/css A3600
 ExpiresByType text/javascript A3600
 ExpiresByType application/x-javascript A3600
</VirtualHost>

And now the trick – you need to add the following to /etc/apache2/httpd.conf

NameVirtualHost *:81

Unless you do all this, Apache will just pick the first virtualhost file in /etc/apache2/sites-enabled and use it for all requests.

Also you need to edit ports.conf and change Apache to listen to port 81:

Listen 81

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

10 thoughts on “Setting up Apache virtual hosts behind Varnish

  1. Pingback: Configure Varnish Cache for Ubuntu and Apache2 VirtualHosts | NW Linux

  2. Thank for info, but that not enough. Because all users want config to varnish coulde ran multi domain.

    In nginx config, has option with this character _
    Character _ meaning all domain

    So, varnish can do that? And I have a question: how can I fix same ip when view via server-status fo Apache?

  3. Hi Mikko Ohtamaa: after searched around on google, but not good news. So, i think why not use follow:

    Varnish caching > Nginx Reverse Proxy (handle connection) > Apache

    Varnish: config same intruction of site or google, simple

    Nginx: config with real ip module:
    set_real_ip_from IP.NUMBER.YOU.NEED;
    real_ip_header X-Forwarded-For;

    Then, config Apache with mod rpaf.

    Yes, amazing, successfull.

  4. Hello

    I am planning to configure Varnish on my server to hide me server ip
    consider my server ip is 66.66.66.66 and I want to configure varnish in such a way, when anyone tries to locate my server ip using any dns checker tool, it should show 127.0.0.1
    to users.

    can you tell me how can i do this. I tried alot and alot but no result.

    help needed.

    Thanks
    Yatin

  5. Hi Yatin,

    What is a DNS checker tool?

    I am not sure if I really understand your use case, why are you doing it or whether it could be possible at all.

  6. Dear Yatin!

    No, you don’t, because that is essential of domain and ip public.

    However, if you want to hide real IP of web server, you need config web server behind reverse proxy. So, at this time, anyone can read ip of reverse proxy only

  7. hello,

    Thanks for reply.. Thats too good option. Can you guide me how to configure reverse proxy using varnish or any other tool

    Thanks
    Yatin

  8. Hi Yatin,

    This blog post is about how you configure reverse Varnish proxy at the front of Apache web server. It’s that simple.

  9. Hi Yatin!
    This Mikko Ohtamaa’s blog has full info you need.

    If you run varnish and apache in same server, you can config apache run other port and varnish run at port 80.

    If you are really need to configurate hide real IP of web server, you have to config in 2 ways:
    _Same server with 2 IP: 1 IP for reverse proxy, 1 IP for apache backend
    _Or need 2 physical server: 1 for reverse proxy, 1 for web server

Leave a Reply

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