Skip to content

Example: sync purging on multiple nginx upstreams #341

@silviu-social1st-ro

Description

@silviu-social1st-ro

Hello, i have 3 nginx hosts behind a HA Proxy instance, for redundancy. By default, is a purge request is received by one of the server, only that server's cache is cleared.

i wrote a workaround so all 3 of them get cleared when an article is updated, but if more people need this, it could be better written into the plugin with some remote actions or stuff.. this is my code:

`

add_action( 'rt_nginx_helper_before_remote_purge_url', function( $url ) {
               define( 'CLUSTER_HOSTS', ['192.168.0.1', '192.168.0.2', '192.168.0.3']);
$headers = [
	'User-Agent' => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
	'cache-control' => 'no-cache',
	'accept' => 'text/html',
	'accept-encoding' => 'gzip, deflate, br',
	'Host' => parse_url(get_site_url())['host']
];
$other_hosts = array_filter(CLUSTER_HOSTS, function($value){
	return trim($value) !== getHostByName(getHostName());
});

// Make the remote GET request for each IP address
foreach ( $other_hosts as $ip_address ) {
	
	$curl_command = 'curl -X GET -s -o /dev/null -w "%{http_code}"  -H "';
	foreach ($headers as $header => $value) {
		$curl_command .= $header . ': ' . $value . '" -H "';
	}
	$curl_command = rtrim($curl_command, ' -H "'). '"'.
	' --resolve '. $headers['Host'] . ':80:'. $ip_address .
	' --resolve '. $headers['Host'] .':443:'. $ip_address .
	' "' . $url . '" > /dev/null 2>&1 &';
	
	// Execute the curl command in the background
	exec($curl_command);
 }}, 10, 2 );

`
I am spawning linux curl threads, as i don't want to delay returning the response to the editor.

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions