Setting and unsetting HTTP headers

Overview

Varnish Cache gives you the ability to modify, add, and remove HTTP headers on the request and response object.

Request Headers

The vcl_recv sub routine is called at the beginning of a request and this is where we will be modifying the request headers. We will be adding the header hello with value of world, and removing the user-agent header.

sub vcl_recv {
    ...

    set req.http.hello = "world";
    unset req.http.user-agent;

    ...
}
The **req.http** object is how to access any request header and is only readable from **vcl_recv** and **vcl_deliver**.

Response Headers

The vcl_backend_response sub routine is called once Varnish Cache has received the response headers from the upstream service. We will be modifying the header cache-control and setting the value to public, max-age=600, and removing the server header.

sub vcl_backend_response {
    ...

    set beresp.http.cache-control = "public, max-age=600";
    unset beresp.http.server;

    ...
}
The **beresp.http** object is how to access any response header and is only readable from **vcl_backend_response** and **vcl_deliver**.