This article describes particular case of Varnish error Backend fetch failed Too many headers. This error happens when there are too many headers being sent to backend server. Here is how you can troubleshoot and fix this error.
First, open the SSH session for your server with Varnish. Then run varnishlog
in order to make recordings of backend requests to a text file:
1 |
varnishlog -q "BereqHeader ~ '^Host: .*\.domain.com'" > bereq.log |
Deactivate your Jetpack and activate it back again. You will see the error in the browser:
1 2 3 4 5 6 7 |
Error 503 Backend fetch failed Backend fetch failed Guru Meditation: XID: 2230744 |
Take note of XID value in the response. It is Varnish transaction ID.
Cancel out from varnishlog
command by typing Ctrl + C in SSH. Open the bereq.log file and search for the XID number in error response.
You will find the backend request information includes something like this:
1 2 3 4 5 |
- BogoHeader Too many headers: Set-Cookie: jetpackS - HttpGarbage "HTTP/1.1%00" - BerespStatus 503 - BerespReason Service Unavailable - FetchError http format error |
This is pretty much self-explanatory. There are too many headers being sent to backend server while activating JetPack.
According to Varnish 4 documentation, there is http_max_hdr parameter, which is:
Maximum number of HTTP header lines we allow in {req|resp|bereq|beresp}.http (obj.http is autosized to the exact number of headers). Cheap, ~20 bytes, in terms of workspace memory.
The default value for the parameter is 64.
Solution
The solution to the issue is to increase the number of headers that Varnish will accept. This is done by tuning the http_max_hdr parameter from the default 64 to 96.
We have to adjust Varnish configuration for this. It’s located in either /etc/varnish/varnish.params, /etc/sysconfig/default or /etc/default/varnish depending on your Linux OS.
You need to find DAEMON_OPTS
setting which controls Varnish startup parameters and add parameter for the maximum number of HTTP headers.
Example, before:
1 2 3 4 5 6 |
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \ -f ${VARNISH_VCL_CONF} \ -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \ -p thread_pool_min=${VARNISH_MIN_THREADS} \ -p thread_pool_max=${VARNISH_MAX_THREADS} \ - ... |
After:
1 2 3 4 5 6 7 |
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \ -f ${VARNISH_VCL_CONF} \ -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \ -p http_max_hdr=96 <- this is the tuned parameter we added -p thread_pool_min=${VARNISH_MIN_THREADS} \ -p thread_pool_max=${VARNISH_MAX_THREADS} \ - ... |
Good luck!