Error 503 backend fetch failed varnish: How to solve this issue in Magento 2

One of the most common errors that Magento 2 users encounter is Error 503 backend fetch failed varnish.  Many Magento users are facing this error and don’t know how to deal with it. In this tutorial, Magesolution will explain to you the nature of the issue and certainly,  all working solutions to resolve these issues.

Causes of the Error 503 backend fetch failed varnish

This error happens when the connection is closed before the response is read by ” Fastly cache servers’. This error may occur when the response includes a missing or invalid content-length header, although other causes may exit:

Here are 3 possible causes of the error which are

  • Length of cache tags used by Magento exceed Varnish’s default value
  • Maintenance. flag file generated by Magento 2
  • Wrong health_check.php location in Varnish config file

And we will guide you on all the possible causes of these problems and of course, the solution to fix the error.

Error 503

Read more : 

Solution 1: Length of cache tags used by Magento exceed Varnish’s default value

In Magento 2, this issue is caused because the length of cache tags is more than 8192 characters. You can solve this error by increasing http_resp_hdr_len value in varnish configuration file.

Solution:

  •  Depend on your OS, go to:

Ubuntu: /etc/default/varnish

CentOS 6.x: /etc/sysconfig/varnish

CentOS 7.x: /etc/varnish/varnish.params

  • Edit the config file and use the following command:
-p http_resp_hdr_len=42000

For example:

DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
         -f ${VARNISH_VCL_CONF} \
         -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
         -t ${VARNISH_TTL} \
         -p thread_pool_min=${VARNISH_MIN_THREADS} \
         -p thread_pool_max=${VARNISH_MAX_THREADS} \
         -p http_resp_hdr_len=42000 \

Save the file and restart your server. The issue should be gone.

Solution 2: Maintenance. flag file created by Magento 2

The second cause of the 503 error may be the var/.maintenance.flag file, which is created by Magento 2 when you install a new module/theme or maintaining, upgradinging, or reconfiguring your Magento 2 site

In this case, you need to open the file manager and rename/remove maintenance.flag file and refresh your Magento 2 website to see if the error is fixed.

Solution 3: Wrong health_check.php location in Varnish config file

Other reasons are also possible that caused the 503 error is wrong health_check.php file location in the varnish configuration file. This commonly occurs even with a fresh installation of Magento 2 (frequently reported in Magento 2.3)

  • By default, the path to health_check.php file in varnish is /pub/health_check.php.
  • In Magento 2, nginx.conf.sample already set root path to /pub => wrong path in varnish configuration file. We will change this to the right path.

Solution:

Move to Varnish configuration file (/etc/varnish/default.vcl) and find these lines

    .probe = {
        .url = "/pub/health_check.php";

Change it to

    .probe = {
        .url = "/health_check.php";

Also, navigate to nginx.conf.sample file in Magento 2 root folder, find the following command:

location ~ (index|get|static|report|404|503)\.php$ {

Add health_check to this line to permit health_check.php file as below

location ~ (index|get|static|report|404|503|health_check)\.php$ {

Remember to save changes and restart varnish and nginx to apply the change. Now your Magento 2 website should be working properly.

Conclusion

Magesolution hopes this tutorial helps you resolve Magento 2 Error 503 Backend fetch failed with varnish cache. If your site still does not work after trying the methods in this tutorial, free to CONTACT US, and we will definitely support you! We not only offer an affordable Magento Development Package for all size and budget but also ensure that it helps your online business grow and sustain.