'Exclude WordPress cart shop when Plus Minus Button add more product not work
When press plus bottom for add more than 1 of product to shopping cart on WordPress, Finally product add to cart after one minute, but I see this error in console:
Request URL: https://abcds.com/cart/ Request Method: POST Status Code: 503 Backend fetch failed
in my vcl I add this value but not work:
# The code below makes sure the AJAX "add to cart" function works
set req.url = regsub(req.url, "add-to-cart=\d+_\d+&", "");
# Do not cache AJAX requests.
if (req.http.X-Requested-With == "XMLHttpRequest") {
return(pass);
}
# Post requests will not be cached
if (req.http.Authorization || req.method == "POST") {
return (pass);
}
# also in vcl_backend_response I add this codes:
if (!(bereq.url ~ "wp-(login|admin)|login|logged-in|my-account|wc-api|resetpass|admin-ajax.php|arisn") && !(bereq.http.cookie ~ "wordpress_logged_in|woocommerce_items_in_cart|resetpass|wp_woocommerce_session_[a-zA-Z0-9]+|wordpress_logged_in_|comment_author|PHPSESSID|woocommerce_cart_hash") && !(beresp.status == 302))
{
unset beresp.http.set-cookie;
set beresp.ttl = 2h;
}
Related images links:
https://i.postimg.cc/hGPTFx0S/header.png
https://i.postimg.cc/BbQ7ztwk/cookies.png
update:
Varnish log:
<< Request >> 20751307
Begin req 20751306 rxreq
Timestamp Start: 1650725983.556894 0.000000 0.000000
Timestamp Req: 1650725983.556894 0.000000 0.000000
ReqStart 127.0.0.1 55316 a0
ReqMethod POST
ReqURL /cart/
ReqProtocol HTTP/1.0
ReqHeader X-Real-IP: 31.14.152.7
ReqHeader X-Forwarded-For: 31.14.152.7
ReqHeader X-Forwarded-Proto: https
ReqHeader X-Nginx: on
ReqHeader Host: bankketab.com
ReqHeader Connection: close
ReqHeader Content-Length: 297
ReqHeader sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Opera";v="85"
ReqHeader accept: text/html, */*; q=0.01
ReqHeader content-type: application/x-www-form-urlencoded; charset=UTF-8
ReqHeader x-requested-with: XMLHttpRequest
ReqHeader sec-ch-ua-mobile: ?0
ReqHeader user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.75
ReqHeader sec-ch-ua-platform: "Windows"
ReqHeader origin: https://bankketab.com
ReqHeader sec-fetch-site: same-origin
ReqHeader sec-fetch-mode: cors
ReqHeader sec-fetch-dest: empty
ReqHeader referer: https://bankketab.com/cart/
ReqHeader accept-encoding: gzip, deflate, br
ReqHeader accept-language: en-US,en;q=0.9
ReqHeader cookie: PHPSESSID=8beb52490b9f7ad5456f8b064a9439a5; woocommerce_recently_viewed=20077%7C20092%7C19496%7C20136; digits_countrycode=98; woocommerce_items_in_cart=1; wp_woocommerce_session_d373d61d3cf9a04133a8550c98d33197=t_df9a955c5d0c76b7f6a9ce2add86d6%7C
ReqUnset X-Forwarded-For: 31.14.152.7
ReqHeader X-Forwarded-For: 31.14.152.7, 127.0.0.1
VCL_call RECV
ReqUnset Host: bankketab.com
ReqHeader host: bankketab.com
ReqURL /cart/
VCL_return pass
VCL_call HASH
VCL_return lookup
VCL_call PASS
VCL_return fetch
Link bereq 20751308 pass
Storage malloc Transient
Timestamp ReqBody: 1650725983.557004 0.000110 0.000110
VCL_Error Uncached req.body can only be consumed once.
Timestamp Fetch: 1650726043.823773 60.266879 60.266769
RespProtocol HTTP/1.1
RespStatus 503
RespReason Backend fetch failed
RespHeader Date: Sat, 23 Apr 2022 15:00:43 GMT
RespHeader Server: Varnish
RespHeader Content-Type: text/html; charset=utf-8
RespHeader Retry-After: 5
RespHeader X-Varnish: 20751307
RespHeader Age: 0
RespHeader Via: 1.1 varnish (Varnish/6.0)
VCL_call DELIVER
RespHeader X-status: 0
RespUnset X-Varnish: 20751307
RespUnset Age: 0
RespUnset Via: 1.1 varnish (Varnish/6.0)
RespHeader X-Configured-By: ServerSetup.co
RespUnset Server: Varnish
RespHeader Server: Nitro
VCL_return deliver
Timestamp Process: 1650726043.823791 60.266897 0.000018
RespHeader Content-Length: 285
RespHeader Connection: close
Timestamp Resp: 1650726043.823839 60.266946 0.000049
ReqAcct 1166 297 1463 231 285 516
End
** << BeReq >> 20751308
-- Begin bereq 20751307 pass
-- Timestamp Start: 1650725983.556958 0.000000 0.000000
-- BereqMethod POST
-- BereqURL /cart/
-- BereqProtocol HTTP/1.0
-- BereqHeader X-Real-IP: 31.14.152.7
-- BereqHeader X-Forwarded-Proto: https
-- BereqHeader X-Nginx: on
-- BereqHeader Content-Length: 297
-- BereqHeader sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Opera";v="85"
-- BereqHeader accept: text/html, */*; q=0.01
-- BereqHeader content-type: application/x-www-form-urlencoded; charset=UTF-8
-- BereqHeader x-requested-with: XMLHttpRequest
-- BereqHeader sec-ch-ua-mobile: ?0
-- BereqHeader user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.75
-- BereqHeader sec-ch-ua-platform: "Windows"
-- BereqHeader origin: https://bankketab.com
-- BereqHeader sec-fetch-site: same-origin
-- BereqHeader sec-fetch-mode: cors
-- BereqHeader sec-fetch-dest: empty
-- BereqHeader referer: https://bankketab.com/cart/
-- BereqHeader accept-encoding: gzip, deflate, br
-- BereqHeader accept-language: en-US,en;q=0.9
-- BereqHeader cookie: PHPSESSID=8beb52490b9f7ad5456f8b064a9439a5; woocommerce_recently_viewed=20077%7C20092%7C19496%7C20136; digits_countrycode=98; woocommerce_items_in_cart=1; wp_woocommerce_session_d373d61d3cf9a04133a8550c98d33197=t_df9a955c5d0c76b7f6a9ce2add86d6%7C
-- BereqHeader X-Forwarded-For: 31.14.152.7, 127.0.0.1
-- BereqHeader host: bankketab.com
-- BereqProtocol HTTP/1.1
-- BereqHeader X-Varnish: 20751308
-- VCL_call BACKEND_FETCH
-- VCL_return fetch
-- BackendOpen 50 boot.cPanelServer 144.76.9.138 8080 144.76.9.138 34358
-- BackendStart 144.76.9.138 8080
-- Timestamp Bereq: 1650725983.557008 0.000050 0.000050
-- Timestamp Beresp: 1650725983.823515 0.266558 0.266507
-- BerespProtocol HTTP/1.1
-- BerespStatus 302
-- BerespReason Found
-- BerespHeader Connection: Keep-Alive
-- BerespHeader Expires: Wed, 11 Jan 1984 05:00:00 GMT
-- BerespHeader Cache-Control: no-cache, no-store, must-revalidate, max-age=0
-- BerespHeader X-Redirect-By: WordPress
-- BerespHeader Location: https://bankketab.com/cart/
-- BerespHeader Set-Cookie: woocommerce_cart_hash=3f7d291f9814ba9163415e03a89629cf; path=/; secure
-- BerespHeader Content-Type: text/html; charset=UTF-8
-- BerespHeader Content-Length: 0
-- BerespHeader Date: Sat, 23 Apr 2022 14:59:43 GMT
-- VCL_call BACKEND_RESPONSE
-- BerespHeader Vary: User-Agent
-- VCL_return retry
-- BackendReuse 50 boot.cPanelServer
-- Timestamp Retry: 1650725983.823536 0.266578 0.000021
-- Link bereq 19813441 retry
-- End
*** << BeReq >> 19813441
--- Begin bereq 20751308 retry
--- Timestamp Start: 1650725983.823536 0.266578 0.000000
--- BereqMethod POST
--- BereqURL /cart/
--- BereqProtocol HTTP/1.1
--- BereqHeader X-Real-IP: 31.14.152.7
--- BereqHeader X-Forwarded-Proto: https
--- BereqHeader X-Nginx: on
--- BereqHeader Content-Length: 297
--- BereqHeader sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Opera";v="85"
--- BereqHeader accept: text/html, */*; q=0.01
--- BereqHeader content-type: application/x-www-form-urlencoded; charset=UTF-8
--- BereqHeader x-requested-with: XMLHttpRequest
--- BereqHeader sec-ch-ua-mobile: ?0
--- BereqHeader user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.75
--- BereqHeader sec-ch-ua-platform: "Windows"
--- BereqHeader origin: https://bankketab.com
--- BereqHeader sec-fetch-site: same-origin
--- BereqHeader sec-fetch-mode: cors
--- BereqHeader sec-fetch-dest: empty
--- BereqHeader referer: https://bankketab.com/cart/
--- BereqHeader accept-encoding: gzip, deflate, br
--- BereqHeader accept-language: en-US,en;q=0.9
--- BereqHeader cookie: PHPSESSID=8beb52490b9f7ad5456f8b064a9439a5; woocommerce_recently_viewed=20077%7C20092%7C19496%7C20136; digits_countrycode=98; woocommerce_items_in_cart=1; wp_woocommerce_session_d373d61d3cf9a04133a8550c98d33197=t_df9a955c5d0c76b7f6a9ce2add86d6%7C
--- BereqHeader X-Forwarded-For: 31.14.152.7, 127.0.0.1
--- BereqHeader host: bankketab.com
--- BereqHeader X-Varnish: 20751308
--- BereqUnset X-Varnish: 20751308
--- BereqHeader X-Varnish: 19813441
--- VCL_call BACKEND_FETCH
--- VCL_return fetch
--- BackendOpen 50 boot.cPanelServer 144.76.9.138 8080 144.76.9.138 34358
--- BackendStart 144.76.9.138 8080
--- FetchError backend write error: 0 (Success)
--- Timestamp Bereq: 1650725983.823596 0.266638 0.000060
--- FetchError Timed out reusing backend connection
--- BackendClose 50 boot.cPanelServer
--- Timestamp Beresp: 1650726043.823712 60.266755 60.000117
--- Timestamp Error: 1650726043.823717 60.266759 0.000004
--- BerespProtocol HTTP/1.1
--- BerespStatus 503
--- BerespReason Service Unavailable
--- BerespReason Backend fetch failed
--- BerespHeader Date: Sat, 23 Apr 2022 15:00:43 GMT
--- BerespHeader Server: Varnish
--- VCL_call BACKEND_ERROR
--- BerespHeader Content-Type: text/html; charset=utf-8
--- BerespHeader Retry-After: 5
--- VCL_return deliver
--- Storage malloc Transient
--- Length 285
--- BereqAcct 2358 297 2655 394 0 394
--- End
Solution 1:[1]
The request body error
As mentioned in the comments, the following log output is quite interesting:
Timestamp ReqBody: 1650725983.557004 0.000110 0.000110
VCL_Error Uncached req.body can only be consumed once.
Timestamp Fetch: 1650726043.823773 60.266879 60.266769
The Uncached req.body can only be consumed once
error implies that your retrying a POST
call and lose the request body in the process.
The following VCL code from your VCL file has logic that could cause this behavior:
if (beresp.http.Location == "https://" + bereq.http.host + bereq.url) {
if (bereq.retries > 1)
{
unset beresp.http.Location;
}
else {
return (retry);
}
}
The fact that the request is a POST
request where the output is a 302
redirect with a Location: https://bankketab.com/cart/
header triggers that part of the VCL.
The problem with retrying a request that has a request body (such as a typical POST
request), is that Varnish doesn't cache the request body. That information is lost when asking for a retry.
The HTTP 503 backend fetch failure
All of this explains the VCL_Error Uncached req.body can only be consumed once.
error.
The actual HTTP 503
error could be something entirely different. However, they could be related.
The logs below refer to a backend write error
:
--- BackendOpen 50 boot.cPanelServer 144.76.9.138 8080 144.76.9.138 34358
--- BackendStart 144.76.9.138 8080
--- FetchError backend write error: 0 (Success)
--- Timestamp Bereq: 1650725983.823596 0.266638 0.000060
--- FetchError Timed out reusing backend connection
--- BackendClose 50 boot.cPanelServer
I'm not 100% sure, but it could be that the backend is waiting for the POST
request body. It doesn't get it, times out and returns the error.
Monitoring backend health
A backend returns an HTTP 503
error when it is not available or when that backend doesn't return the expected result in a timely fashion.
Luckily there's a varnishlog
command that can help us check the status of a pre-defined health probe.
Your VCL code happens to feature a backend_healthcheck
health probe that polls the backend every 5 seconds.
Please run the following command to check the general health of your backend:
varnishlog -g raw -i backend_health
Every 5 seconds there should be a new log line with the status of the health check. Run it for a minute or so and see what the status is. If the backend is healthy, there is no critical problem with the backend. This can also help us conclude that the problem you're experiencing is really related to the absence of the request body in the retry.
Solving the problem
My advice is to use the std.cache_req_body()
function that is described in https://varnish-cache.org/docs/6.0/reference/vmod_generated.html#func-cache-req-body to ensure the request body services a return(retry)
in the VCL code.
Here's an oversimplified VCL example that features this function:
sub vcl_recv {
if(std.cache_req_body(1KB)) {
std.log("Request body accessible");
} else {
std.log("Request body not accessible");
}
}
A boolean value is returned from std.cache_req_body()
describing whether or not retrieving the request body from cache was successful. You could return a synthetic error in the else
path if you want.
There's no guarantee that caching the request body will solve the issue, but there are indications in the log that the failure can be caused by the absence of the request body. In any case it makes sense to run
varnishlog -g raw -i backend_health
to figure out what the general health is of your backend.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Thijs Feryn |