0
votes

I've just installed varnish on my development server and it's running without changing any configuration. So now it just asks Apache for the response and passes it back.

Well, I'm a newbie and I'm trying to cache javascript, css and images to test varnish. My problem is that if I write return (lookup); in vcl_recv gives me error on service varnish restart!!

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
  .host = "127.0.0.1";
  .port = "80";
}

sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
#hash_data(req.url);
#if (req.http.host) {
#    hash_data(req.http.host);
#} else {
#    hash_data(server.ip);
#}
return (lookup);
}

sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.

 }

sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
}

This configuration in default.vcl gives me the next error on restart:

Job for varnish.service failed. See 'systemctl status varnish.service' and 'journalctl -xn' for details.

Help me please!!

2

2 Answers

0
votes

You can do something like:

sub vcl_recv {
  if (req.url ~ "(?i)\.(jpeg|jpg|png|gif|ico|js|css)$") {
      unset req.http.Cookie;
      return (hash);
  } else {
      return (pass);
  }
}

For an extended answer, you might want to look at the answer for Varnish 3 in https://serverfault.com/a/551283/426146.

0
votes

The problem is that in default.vcl the port is 80. The port must be the port that your web server is listening for example 8080. The web server must be configured in 8080. And in Debian your /etc/systemd/system/varnish.service file has the port that you have to change to 80

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,512m

Then with your Browser you are connecting to 80 in varnish and the varnish asking web server from 8080 the pages and files (cached after first hit).

cp /lib/systemd/system/varnish.service /etc/systemd/system/

systemctl daemon-reload && systemctl status varnish

And is starting to use the file and after that you ca enter your code in /etc/varnish/default.vcl