Debugging Magento 2 Varnish Cache Issues: A Comprehensive Guide
As an experienced senior developer, I understand your frustration when encountering issues like the “Backend fetch failed” error on a Magento 2 site with varnish cache. Let’s dive into this problem and find a solution that works for you.
Understanding Your Configuration
You’re using Apache v2.4.37 and varnish cache with a vanilla Magento 2.3.0 website deployed from the Bitnami AWS Magento image. This configuration is quite standard, but the issue lies in how you’ve set up your virtual hosts and redirect rules.
The Culprit: Incorrect Rewrite Rules
Upon reviewing your configuration files, I noticed an inconsistency in your rewrite rules. The problematic line:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://tyanabelle.com%{REQUEST_URI} [R=301,L,NE]
Is causing the 503 errors when the non-HTTPS or www subdomain is accessed. The issue here is that Magento requires an absolute path for the base URL during the rewrite process.
The Solution: Update Your Rewrite Rules
To resolve this, we need to update your RewriteRule line in the virtual hosts file:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://tyanabelle.com%{REQUEST_URI} [R=301,L,NE,E=redirect:%{REQUEST_URI}]
I’ve added the E=redirect flag to store the original request URI in an environment variable. This ensures that Magento has access to the full URI when processing redirects.
Ensuring Secure Redirects
To redirect all subdomains and non-HTTPS calls to HTTPS, you can use a combination of Apache and Nginx settings:
- Apache (Virtual Host):
- Ensure your
RewriteRuleis set up as shown above. - Include the following directive in your virtual host file to force SSL for all subdomains:
- Ensure your
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName tyanabelle.com
DocumentRoot /var/www/magento
Redirect permanent / https://tyanabelle.com/
</VirtualHost>
</IfModule>
- Nginx (Proxy Pass):
- Set up a proxy pass from Nginx to the Apache server for handling HTTPS requests:
server {
listen 80;
server_name tyanabelle.com www.tyanabelle.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2;
server_name tyanabelle.com;
proxy_pass http://internal_apache_server;
}
Replace internal_apache_server with the address of your Apache server.
Conclusion
In conclusion, to enable HTTPS redirects for all subdomains and non-HTTPS calls on your Magento site without encountering “Backend fetch failed” errors:
- Update your Apache rewrite rule as shown above.
- Configure your Apache virtual host to handle SSL redirection.
- Set up a proxy pass from Nginx to the Apache server for handling HTTPS requests.
Remember, it’s essential to thoroughly test each change in an isolated environment before deploying updates to a production site.
If you found this guide helpful, consider exploring our other Magento-related articles and services on LaravelCompany. We specialize in providing high-quality development resources and solutions for the Laravel ecosystem.