Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: sync purging on multiple nginx upstreams #341

Open
silviu-social1st-ro opened this issue May 16, 2024 · 0 comments
Open

Example: sync purging on multiple nginx upstreams #341

silviu-social1st-ro opened this issue May 16, 2024 · 0 comments

Comments

@silviu-social1st-ro
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant