Skip to content

Implement A Way To Shim The Varnish Config #54

@null93

Description

@null93

This came up when Josh noticed that the Magento team was only updating bugs in the Varnish config for the newest version of Magento and did not try to implement the change for older minor versions. This is obviously a problem. Josh suggested that we implement this fix as part of our Module, but this is severely out of scope for the module. Instead we should optionally allow a user to generate a config that supports "shims". Consider the following example VCL file located in /etc/varnish/default.vcl:

# >jetrails_

vcl 4.0;

import std;

include "shims.vcl";

backend default {
	.host = "localhost";
	.port = "8080";
}

sub vcl_recv {
	call shim_recv_start;
	# Logic in between
	call shim_recv_end;
}

sub vcl_hash {
	call shim_hash_start;
	# Logic in between
	call shim_hash_end;
}

sub vcl_backend_response {
	call shim_backend_response_start;
	# Logic in between
	call shim_backend_response_end;
}

sub vcl_deliver {
	call shim_deliver_start;
	# Logic in between
	call shim_deliver_end;
}

sub vcl_hit {
	call shim_hit_start;
	# Logic in between
	call shim_hit_end;
}

Also consider the included shims file in /etc/varnish/shims.vcl:

# >jetrails_
#
# Please know what you are doing before making any changes. Any and all changes
# should include a description of the issue that the given shim solves as well
# as any helpful references that should be referenced.

sub shim_recv_start {
	# Fixes Varnish 6's too many restarts issue
	# https://github.com/magento/magento2/pull/28137
	if ( req.restarts > 0 ) {
		set req.hash_always_miss = true;
	}
}

sub shim_recv_end {}
sub shim_hash_start {}
sub shim_hash_end {}
sub shim_backend_response_start {}
sub shim_backend_response_end {}
sub shim_deliver_start {}
sub shim_deliver_end {}
sub shim_hit_start {}
sub shim_hit_end {}

# Please Note: Native Varnish sub-routines can also be shim'ed into the parent
# config as long as they are not already defined.
#
# For example, even though Magento does not use the 'vcl_synth' sub-routine, we
# can still define it here and shim it into the official Magento Varnish VCL:
#
sub vcl_synth {
	set resp.http.Content-Type = "text/html; charset=utf-8";
	set resp.http.Retry-After = "5";
	synthetic ({"
		<!DOCTYPE html>
		<html>
			<head>
				<title>"} + resp.status + " " + resp.reason + {"</title>
			</head>
			<body>
				<h1>Error "} + resp.status + " " + resp.reason + {"</h1>
				<p>"} + resp.reason + {"</p>
				<h3>Guru Meditation:</h3>
				<p>XID: "} + req.xid + {"</p>
				<hr>
				<p>Varnish Cache Server</p>
			</body>
		</html>
	"});
	return (deliver);
}

This will not only allow the customer to update their Varnish config with every Magento update and Module update, but they will be able to retain their customizations without having to reimplement them down the road when an update to the config is needed.

This is currently hypothetical and will need to be tested to ensure everything runs correctly.

Metadata

Metadata

Assignees

Labels

effort: mediumWill take a bit of effort to completeenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions