How Does vcl_error Work?
Fastly uses errors to handle many different circumstances, e.g. creating different results for requests. Let’s look at what this process looks like if we want to redirect a request for www.example.com
to example.com
.
When the request comes in to vcl_recv
, we can catch it using a conditional, something like:
if (req.http.host ~ “^www.”) {
error 607 “Fastly www. Redirect”;
}
Then in vcl_error
, we can make the changes that we want to our URL:
if (obj.status == 607) {
set obj.status = 301;
set obj.response = "Moved Permanently";
set obj.http.Location = regsub(req.http.host, "^www\.","") req.url;
synthetic {""};
return (deliver);
}
Several important things to note here:
- The error code generated in
vcl_recv
(i.e. 607 in this instance):- Fastly strongly encourages using custom error codes in the 600’s or 700’s. This will avoid confusion with other error codes in the future.
- If a customer is using the UI to create the error, the number will be automatically generated for them.
- This is a status code used to look up what to do in the event of that error. If you want to send a different status code back to the browser, you can set
obj.status
invcl_error
orvcl_deliver
and that will override the status code sent in with the error.
- The response in
vcl_recv
(i.e. “Fastly www. Redirect” in this instance) isn’t required, but it should be.- This status code can help out significantly in troubleshooting.
- If you want to make your
vcl_error
easier to read in the future, you can test forobj.response
; e.g.if (obj.response == “Fastly www. Redirect”) { }
.
- You can create a synthetic response for errors as well.
- Using the UI.
- Or, you can type your HTML into the brackets at
synthetic {""};
. Find more information here
- Errors always end with
return (deliver);
- Depending on how you manipulated the request in your error, you may need to make some accommodations in
vcl_deliver
as well. - If you don’t run a return, it falls through to master vcl. Things can go weird if that happens.
- Depending on how you manipulated the request in your error, you may need to make some accommodations in
If you have any questions, support would love to troubleshoot with you. Email us at support@fastly.com.
-
Hi Squee,
We have a requirement to redirect any requests only from https://www.pn.com/ or https://www.pn.com/default.aspx to https://www.dn.com/pn/
I have tried various methods (vcl & error) in the fastly fiddle and did not work as per the requirement.
Here dn.com is the magento2 website where we use Fastly CDN.
-U
-
Hi Udaya,
Thanks for reaching out.
We're happy to look at this with you, but I suspect we'll need more details to provide you with a solution.
As for the fiddle, the host on the fiddle is some variation on `exec<number>.fiddle.fastlydemo.net`. If you want to write VCL to be able to test redirects coming in from another host, try setting that host at the top of `vcl_recv`. That would look something like:
set req.http.host = "www.pn.com";
You would only want to do this in the fiddle to create the testing environment you need. Please don't put this line in your code unless you intend to rewrite the host for your origin to find.
If you have further questions, please don't hesitate to open a ticket with support by emailing support@fastly.com.
-
Hi Squee
Thank you so much Squee, This was a real life-saver for me.
And this worked perfectly fine on my magento-cloud where we have fastly.
We recently migrated a magento1 website from their old server to magento2 in magento-cloud.
So, I was in a situation where I needed a custom URL redirect for:
`mcstaging.magento-cloud.com/us/<anything>` should be redirected to `mcstaging-us.magento-cloud.com/us/<anything>`
I basically made use of your rule:
in `vcl_recv`
# 301 Redirects
if (req.http.host == "mcstaging.magento-cloud.com" && req.url ~ "^/us/(.*)") {
error 607 "Custom RegEx 301 Redirect";
}in `vcl_error`
# 301 Redirects
if (obj.status == 607) {
set obj.status = 301;
set obj.response = "Moved Permanently";
set obj.http.Location = "https://mcstaging-us.magento-cloud.com/" req.url;
synthetic {""};
return (deliver);
}This helped me with redirecting all old `/us/` store URL's to be redirected to new US store domain `mcstaging-us.magento-cloud.com`
This documentation was really helpful.
Thanks,
Anish
Please sign in to leave a comment.
Comments
3 comments