Shared Web Hosting con Varnish

Hemos decidido montar un OpenVZ sobre un servidor físico para ofrecer VPSs a nuestro clientes. Cada cliente dispondrá de un servidor virtual (container) para construir su web. ¿Cómo podemos compartir el web hosting entre todos los VPSs?

De entre todas las opciones posibles vamos a hacerlo con Varnish configurado como Proxy

Segun Wikipedia:

Varnish Cache es un acelerador de aplicaciones web, también conocido como caché de proxy HTTP inversa. Se instala delante de cualquier servidor HTTP y se configura para almacenar en caché una copia del recurso solicitado. Ideado para aumentar el rendimiento de las aplicaciones web.

Lo primero que debemos hacer es instalar varnish

# apt-get install varnish

Debemos ajustar los parámetros para iniciar el demonio:

# vim /etc/default/varnish

Incluimos la configuración necesaria:

VARNISH_RUN_USER=varnish
VARNISH_RUN_GROUP=varnish

START=yes

# Maximum number of open files (for ulimit -n)
NFILES=131072
# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000
# Maximum number of threads (for ulimit -u)
NPROCS="unlimited"
# Maximum size of corefile (for ulimit -c). Default in Fedora is 0
# DAEMON_COREFILE_LIMIT="unlimited"

RELOAD_VCL=1

# # Should probably change this
VARNISH_VCL_CONF=/etc/varnish/default.vcl

# # Not setting VARNISH_LISTEN_ADDRESS makes Varnish listen on all IPs on this box
# # (Both IPv4 and IPv6 if available). Set manually to override this.
# VARNISH_LISTEN_ADDRESS=
VARNISH_LISTEN_PORT=80

# # Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082

# # Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret

# # The minimum number of worker threads to start
VARNISH_MIN_THREADS=50

# # The Maximum number of worker threads to start
VARNISH_MAX_THREADS=5000

# # Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120

# Best option is malloc if you can. malloc will make use of swap space smartly if
# you have it and need it.
VARNISH_STORAGE_TYPE=malloc

# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=2G

VARNISH_STORAGE="${VARNISH_STORAGE_TYPE},${VARNISH_STORAGE_SIZE}"

# # Default TTL used when the backend does not specify one
VARNISH_TTL=60

# # DAEMON_OPTS is used by the init script.  If you add or remove options, make
# # sure you update this section, too.
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} 
             -f ${VARNISH_VCL_CONF} 
             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} 
             -t ${VARNISH_TTL} 
             -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} 
             -u ${VARNISH_RUN_USER} -g ${VARNISH_RUN_GROUP} 
             -S ${VARNISH_SECRET_FILE} 
             -s ${VARNISH_STORAGE}"

A continuación definimos la configuración del proxy:

backend default {
  .host = "127.0.0.1";
  .port = "80";
}

backend client1 {
        .host = "192.168.1.6";
        .port = "80";
}

backend client2 {
        .host = "192.168.1.7";
        .port = "80";
}

sub vcl_recv {
    set req.grace = 10s;
    
    if (req.http.host ~ "client1.example.com") {
      set req.backend = client1;
    } else if (req.http.host ~ "client2.example.com") {
      set req.backend = client2;
    } else {
      set req.backend = default;
    }
[...]

Comprobamos que la sintaxis está correcta:

# varnishd -C -f /etc/varnish/default.vcl

Si no devuelve ningún error, podemos reiniciar el demonio:

# service varnish restart
This entry was posted in Apache, Linux, Tips, Virtualización and tagged , , , . Bookmark the permalink.