Varnish server in front of nginx server with multiple virtualhosts

Posted by Garreth 00 on Server Fault See other posts from Server Fault or by Garreth 00
Published on 2013-11-13T14:31:06Z Indexed on 2013/11/13 15:59 UTC
Read the original article Hit count: 214

Filed under:
|
|

I have tried to search for a solution for this, but can't find any documentation/tips on my specific setup.

My setup:

Backendserver: ngnix: 2 different websites (2 top domains) in virtualenv, running gunicorn/python/django Backendserver hardware(VPS) 2gb ram, 8 CPU

Databaseserver: postgresql - pg_bouncer
Backendserver hardware (VPS) 1gb ram, 8 CPU

Varnishserver: only running varnish
Varnishserver hardware (VPS) 1gb ram, 8 CPU

I'm trying to set up a varnish server to handle rare spike in traffic (20 000 unique req/s) The spike happens when a tv program mention one of the sites.

What do I need to do, to make the varnish server cache both sites/domains on my backendserver?

My /etc/varnish/default.vcl :

backend django_backend {
    .host = "local.backendserver.com";
    .port = "8080";
}

My /usr/local/nginx/site-avaible/domain1.com

upstream gunicorn_domain1 {
    server unix:/home/<USER>/.virtualenvs/<DOMAIN1>/<APP1>/run/gunicorn.sock fail_timeout=0;

}

server {
    listen 80;
    listen 8080;
        server_name domain1.com;
        rewrite ^ http://www.domains.com$request_uri? permanent;
}

server {
    listen 80 default_server;
    listen 8080;

    client_max_body_size 4G;
    server_name  www.domain1.com;

    keepalive_timeout 5;

    # path for static files
    root /home/<USER>/<APP>-media/;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;

      if (!-f $request_filename) {
        proxy_pass http://gunicorn_domain1;
        break;
      }
    }

}

My /usr/local/nginx/site-avaible/domain2.com

upstream gunicorn_domain2 {
    server unix:/home/<USER>/.virtualenvs/<DOMAIN2>/<APP2>/run/gunicorn.sock fail_timeout=0;

}

server {
    listen 80;
    listen 8080;
        server_name domain2.com;
        rewrite ^ http://www.domains.com$request_uri? permanent;
}

server {
    listen 80;
    listen 8080;

    client_max_body_size 4G;
    server_name  www.domain2.com;

    keepalive_timeout 5;

    # path for static files
    root /home/<USER>/<APP>-media/;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;

      if (!-f $request_filename) {
        proxy_pass http://gunicorn_domain2;
        break;
      }
    }

}

Right now, If I try the Ip of the varnishserver I only get served domain1.com. Would everything be correct if I change the DNS of the two domain to point to the varnishserver, or is there extra setup before it would work?

Question 2: Do I need a dedicated server for varnish, or could I just install varnish on my backendserver, or would the server run out of memory quick?

© Server Fault or respective owner

Related posts about nginx

Related posts about virtualhost