Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have three servers in the backend, with a round robin director.

I would like varnish to write a X-Server-By header with the name or ip of the server from which the response was fetched, how can this be done?

share|improve this question

4 Answers 4

In Varnish 3.0.2 I use:

sub vcl_fetch {
    set beresp.http.X-Backend = beresp.backend.name;
}

Works perfectly.

share|improve this answer
1  
For Varnish 4, vcl_fetch has been renamed vcl_backend_response. Otherwise this works there too. –  Pete Jul 31 at 21:50
sub vcl_fetch {
    set obj.http.X-Backend = req.backend;
}

Tested on 2.0.6 in my (non round robin) setup. It should give you something to go on.

share|improve this answer
1  
that just displays the backend id, not the individual server –  hoberion Nov 2 '10 at 11:26
    
In Varnish 3+ this should be: set beresp.http.X-Backend = req.backend;, however it will still only report the backend's id so it will not be that useful with round-robin backends. –  Martijn Heemels Jul 4 '12 at 13:39
    
This is not the way to do it. Set the header on the backend. –  Pax Jul 11 '12 at 19:13

You can set the header on your web server eg x-host: server2 and then varnish will return that as part of the response to a cache hit.

I have this working on apache and it works perfectly.

share|improve this answer
    
Do it this way, trying to set the header in Varnish doesn't make sense. Set it at the backend itself and Varnish will pass it on to the client. –  Pax Jul 11 '12 at 19:11

The following is not tested, but might get you started:

sub vcl_deliver {
    set resp.http.X-Served-By = server.hostname;
    deliver;
}
share|improve this answer
    
that gives you the name of het varnish server –  hoberion Nov 2 '10 at 11:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.