Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Last active April 5, 2019 15:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mttjohnson/6fd5e6906d5216b573ebda225f903893 to your computer and use it in GitHub Desktop.
Save mttjohnson/6fd5e6906d5216b573ebda225f903893 to your computer and use it in GitHub Desktop.
Mock Varnish Backend Request
# Consider filtering the varnishlog output
# https://feryn.eu/blog/varnishlog-measure-varnish-cache-performance/
varnishlog -i "RespHeader,Req*" -X "RespHeader:(x|X)-" -I "timestamp:Resp" -x reqprotocol,reqacct -g request
# Capture the log output from varnishlog and look for BereqHeader
# -n name is how you can specify a specific named instance of varnish
# remove -n if you just want to access the default instance
varnishlog -n stage
# Use those values in building a curl request
# * << BeReq >> 3
# - Begin bereq 2 fetch
# - Timestamp Start: 1482951822.860979 0.000000 0.000000
# - BereqMethod GET
# - BereqURL /the-url-path-i-requested
# - BereqProtocol HTTP/1.1
# - BereqHeader X-Real-IP: 127.0.0.1
# - BereqHeader X-Forwarded-Port: 80
# - BereqHeader X-Forwarded-Proto: http
# - BereqHeader Host: example.com
# - BereqHeader Pragma: no-cache
# - BereqHeader Upgrade-Insecure-Requests: 1
# - BereqHeader User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36
# - BereqHeader Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
# - BereqHeader Referer: http://example.com/the-url-path-i-requested
# - BereqHeader Accept-Language: en-US,en;q=0.8
# - BereqHeader Cookie: PHPSESSID
# - BereqHeader X-Forwarded-For: 127.0.0.1, 127.0.0.1
# - BereqHeader Accept-Encoding: gzip
# - BereqHeader X-Varnish: 3
# - VCL_call BACKEND_FETCH
# - VCL_return fetch
# - BackendOpen 23 boot.default 127.0.0.1 8081 127.0.0.1 46958
# - BackendStart 127.0.0.1 8081
# - Timestamp Bereq: 1482951822.861428 0.000449 0.000449
# - FetchError http read error: overflow
# - BackendClose 23 boot.default
# - Timestamp Beresp: 1482951824.284536 1.423558 1.423108
# - Timestamp Error: 1482951824.284547 1.423568 0.000010
# - BerespProtocol HTTP/1.1
# - BerespStatus 503
# - BerespReason Service Unavailable
# - BerespReason Backend fetch failed
# - BerespHeader Date: Wed, 28 Dec 2016 19:03:44 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
# - ObjProtocol HTTP/1.1
# - ObjStatus 503
# - ObjReason Backend fetch failed
# - ObjHeader Date: Wed, 28 Dec 2016 19:03:44 GMT
# - ObjHeader Server: Varnish
# - ObjHeader Content-Type: text/html; charset=utf-8
# - ObjHeader Retry-After: 5
# - Length 278
# - BereqAcct 910 0 910 32767 0 32767
# - End
# Construct a curl command to simulate the backend request from Varnish
# Check for esi includes in backend response
# This is nice for checking the ESI include URL and testing it if it's missing.
# If the ESI include fails due to a 500 error from the backend it may just
# not have content where you expect it, like the nav section of the page is missing.
curl -s \
-H "X-Real-IP: 127.0.0.1" \
-H "X-Forwarded-Port: 443" \
-H "X-Forwarded-Proto: https" \
-H "Host: example.com" \
-H "Pragma: no-cache" \
-H "Upgrade-Insecure-Requests: 1" \
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
-H "Referer: http://example.com/the-url-path-i-requested" \
-H "Accept-Language: en-US,en;q=0.8" \
-H "Cookie: PHPSESSID" \
-H "X-Forwarded-For: 127.0.0.1, 127.0.0.1" \
-H "X-Varnish: 3" \
http://127.0.0.1:8081/ \
| grep "esi:include"
# Gets the headers only
# nice for evaluating X-Magento-Tags values that get passed to Varnish
curl -I \
-H "X-Real-IP: 127.0.0.1" \
-H "X-Forwarded-Port: 80" \
-H "X-Forwarded-Proto: http" \
-H "Host: example.com" \
-H "Pragma: no-cache" \
-H "Upgrade-Insecure-Requests: 1" \
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
-H "Referer: http://example.com/the-url-path-i-requested" \
-H "Accept-Language: en-US,en;q=0.8" \
-H "Cookie: PHPSESSID" \
-H "X-Forwarded-For: 127.0.0.1, 127.0.0.1" \
-H "Accept-Encoding: gzip" \
-H "X-Varnish: 3" \
http://127.0.0.1:8081/the-url-path-i-requested
# To get the size of the headers and body of the response
# Good to determine the header size in the event max_http_header_size needs to be increased
# Example: A category page containing thousands of products ends up causing X-Magento-Tags to get ridiculously long.
curl -s -w \%{size_header}:\%{size_download} -o /dev/null \
-H "X-Real-IP: 127.0.0.1" \
-H "X-Forwarded-Port: 80" \
-H "X-Forwarded-Proto: http" \
-H "Host: example.com" \
-H "Pragma: no-cache" \
-H "Upgrade-Insecure-Requests: 1" \
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
-H "Referer: http://example.com/the-url-path-i-requested" \
-H "Accept-Language: en-US,en;q=0.8" \
-H "Cookie: PHPSESSID" \
-H "X-Forwarded-For: 127.0.0.1, 127.0.0.1" \
-H "Accept-Encoding: gzip" \
-H "X-Varnish: 3" \
http://127.0.0.1:8081/the-url-path-i-requested
# Gets the request contents and all verbose details (just remember to remove the "Accept-Encoding: gzip" header.
curl -v \
-H "X-Real-IP: 127.0.0.1" \
-H "X-Forwarded-Port: 80" \
-H "X-Forwarded-Proto: http" \
-H "Host: example.com" \
-H "Pragma: no-cache" \
-H "Upgrade-Insecure-Requests: 1" \
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
-H "Referer: http://example.com/the-url-path-i-requested" \
-H "Accept-Language: en-US,en;q=0.8" \
-H "Cookie: PHPSESSID" \
-H "X-Forwarded-For: 127.0.0.1, 127.0.0.1" \
-H "X-Varnish: 3" \
http://127.0.0.1:8081/the-url-path-i-requested
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment