Investigating Varnish errors
When Varnish is enabled, two common HTTP errors can occur: 502 Bad Gateway and 503 Service Unavailable. Both are related to how NGINX and Varnish handle response headers and buffers, but they have different causes and solutions. This article guides you through identifying and resolving both.
502 Bad Gateway
What causes it?
One common cause of a 502 Bad Gateway error with Varnish enabled is that NGINX receives response headers from Varnish that exceed its configured buffer sizes.
This can happen after enabling Varnish or after a change that increases the size of response headers, for example:
large cookies
many
Set-Cookieheadersadditional custom response headers
Step 1: Check the NGINX Error Log
Inspect /var/log/nginx/error.log and look for the following message:
upstream sent too big header while reading response header from upstream
If this message is present, increase the NGINX buffer sizes used for upstream.
Step 2: Solution
Create a custom NGINX config file at ~/nginx/server.header_buffer with the following content:
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
This increases the buffer sizes NGINX uses when reading response headers from upstream (Varnish), which resolves the “too big header” issue in the vast majority of cases.
Tip
After creating the file, NGINX will be reloaded automatically