2

I setup Varnish for Drupal 7.x; when I go to settings.php to uncomment the following line, and I try to login as administrator, I am just redirected to the same page; nothing happens. No idea what could I have done wrong.

$conf['reverse_proxy'] = TRUE;

3 Answers 3

2

I'd like more details. To start, here's what I have in my settings.php:

/**
 * Varnish settings
 */
$conf['cache_backends'][] = 'sites/all/modules/varnish/varnish.cache.inc';
$conf['cache_class_cache_page'] = 'VarnishCache';
$conf['reverse_proxy'] = true;
$conf['cache'] = 1;
$conf['cache_lifetime'] = 0;
$conf['page_cache_maximum_age'] = 21600;
$conf['reverse_proxy_header'] = 'HTTP_X_FORWARDED_FOR';
//$conf['page_cache_invoke_hooks'] = false;
$conf['reverse_proxy_addresses'] = array('127.0.0.1');
$conf['omit_vary_cookie'] = true;
$conf['page_cache_invoke_hooks'] = FALSE;

All of that might not be necessary, like $conf['page_cache_invoke_hooks'] = FALSE - but it's working for me

First, did you set Varnish to port 80 and Apache to port 8080? First try to access the site via http://example.com:8080 to see if Apache is still serving pages. Another way to check for servers working is by opening terminal via ssh and typing 'netstat -anp | grep httpd'. Here is my output:

netstat -anp | grep httpd
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      324/httpd

netstat -anp | grep varnish
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      24841/varnishd      
tcp        0      0 127.0.0.1:6082              0.0.0.0:*                   LISTEN      24840/varnishd      

You can see that apache (httpd) is working on :8080 and varnishd (d for daemon i.e. service) is running on :80 and :6082 where :80 is varnish and :6082 is its backend.

If you CAN see site on :8080 and you have varnish running on :80 and :6082 you should go to the varnish drupal config page via http://example.com:8080/admin/config/development/varnish

From there you should select the correct Varnish version set, the Varnish Control Terminal set to 127.0.0.1:6082, and your Varnish Control Key set. You can find this by seeing the output of the secret file. In CentOS you can do this by: cat /etc/varnish/secret - this will give you a 30ish char long string to input. At the bottom of this settings page you will find "Status: Varnish Running" hopefully.

If you have all of this working and still can't get the site to show, it will be giving you an output noting Varnish and a Guru Meditation. This shows you that varnish is indeed running but just isn't configured correctly, most likely in your .vcl file. If no guru meditation and site not working, you probably haven't set the sysconfig correctly. Edit - again CentOS - /etc/sysconfig/varnish using vi or nano. I have the following uncommented:

// using Alternative 2, Configuration with VCL
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s file,/var/lib/varnish/varnish_storage.bin,1G \

Check that the .vcl file is correctly referenced and that the port (:6082) is the same as what you found with netstat above.

Finally, if you are getting the Varnish Guru Meditation the .vcl is the issue. In this case here's some of what I have for the .vcl:

BEGINNING OF FILE ->
backend default {
  .host = "127.0.0.1";
  .port = "8080";
  .connect_timeout = 600s;
  .first_byte_timeout = 600s;
  .between_bytes_timeout = 600s;
}

sub vcl_recv {
//TRY THIS AT BEGINNING ->
return(pass)
..
}

The return(pass) will pass EVERYTHING and thus Varnish won't actually be caching anything, but you can use for diagnosis. Remember after changing anything varnish related you need to restart varnish by: service varnish restart - if you .vcl has any errors it won't restart.

2

Apparently your login/register pages are served by Varnish and your vcl configuration strips SESS cookie (seen in many examples). Here is what you should add to your vcl config (Varnish 2.x), in vcl_recv function:

# Do not cache these paths.
if (req.url ~ "^/status\.php$" ||
    req.url ~ "^/update\.php$" ||
    req.url ~ "^/ooyala/ping$" ||
    req.url ~ "^/admin/build/features" ||
    req.url ~ "^/info/.*$" ||
    req.url ~ "^/flag/.*$" ||
    req.url ~ "^.*/ajax/.*$" ||
    req.url ~ "^.*/ahah/.*$" ||
    req.url ~ "^/user/.*$") {
    return (pass);
}     

This will always pass 'user/' related pages to your backend (Apache/nginx/your web server of choice) and Varnish will never serve cached versions for this pages.

0

I guess varnish strips your session cookies. I guess you are using D7 or pressflow. Did you install the Varnish module?

2
  • I am using D7.. No, I didnt... I guess I Should?:) Oct 19, 2012 at 6:23
  • 1
    I installed and enabled Varnish through drush (cause can't login) but I still can't login.. I added the piece of code in the settings.php as suggested. I guess Varnish is not yet widely used in D7? (out of the box caching works just as well?) Oct 19, 2012 at 6:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.