magento 2 hosting with cloudways
Fixing magento issues

[SOLVED] Magento 2 Error 503 Backend fetch failed with varnish cache

Pinterest LinkedIn Tumblr

Magento 2 comes with many complicated features which makes it one of the most powerful open source Ecommerce platform at the moment. However, due to its complexity, there are many type of error may occur when working with Magento.

One of the most common error that Magento 2 user encounter is 503 Backend fetch failed with varnish cache

magento 2 503 error

In this series of tutorials on fixing Magento 2 issues, I will guide you all the possible causes of the error and of course, the solution to resolve the error.

Note: In case you see the error message: Error 503 service temporarily unavailable, read this tutorial to learn how to fix it => How to fix Magento 2 Error 503 service temporarily unavailable

Possible causes of the error and solution

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

If your store has a large number of products (more than 400), the error may happen because the length of cache tags used by Magento 2 exceed the default value of Varnish. The default value of Varnish is 8192 bytes, which mean it is only stable for a store with less than 400 products. (safe http_resp_hdr_len value = product number multiplied by 21)

Solution

We will change http_resp_hdr_len value in Varnish configuration file. (Maximum length of any HTTP backend response header – read here for more detail)

The path to varnish configuration file is

  • Ubuntu: /etc/default/varnish
  • Centos 6 : /etc/sysconfig/varnish
  • Centos 7: /etc/varnish/varnish.params

Edit the file with a text editor and change the http_resp_hdr_len value (located in line 101 after thread_pool_max )

-p http_resp_hdr_len=8192 \

to larger value (for example, 70000 bytes should work well for 3000+ products)

-p http_resp_hdr_len=70000 \

Also, change http_resp_size (Maximum number of bytes of HTTP backend response) to the value that fit the increased value of http_resp_hdr_len , for example 10000 http_resp_size should be good with 70000 http_resp_hdr_len

-p http_resp_size=100000 \

It should be like this after you make change

http_resp_hdr_len

Finally, save file and restart Magento 2 server to apply changes.

2. Wrong health_check.php location in Varnish config file

The other possibility that caused 503 error is wrong health_check.php file location in varnish configuration file. This usually happens 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 correct path.

Solution

Go 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, go to nginx.conf.sample file in Magento 2 root folder, find the following lines:

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

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

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

Save changes and restart varnish and nginx to apply change. Now your site should be working properly

3. Maintenance.flag file generated by Magento 2

Another cause of the 503 error may be the var/.maintenance.flag file, which is generated by Magento 2 when you install new extensions/theme or maintaining, upgrading, or reconfiguring your site.

maintenance.flag

Solution

Open file manager and rename/remove maintenance.flag file and refresh your website to see if the error is solved.

Conclusion

I hope 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 solution in this tutorial, please drop a line in the comment section with link to your site. I will examine and help you fix if possible.

Hung Tran is the main editor of magentip.com, he loves to write about everything related to Magento 2

2 Comments

  1. Why does health check mail varnish fail? I have the same issue over the last few magentos since 2.1

    • What was the full error message?
      You should check if health_check.php location was wrong in Varnish config file

Write A Comment