Skip to content

Instantly share code, notes, and snippets.

@sthembi
Forked from JamesPaden/reverse-proxy.php
Last active December 21, 2020 12:53
Show Gist options
  • Save sthembi/b56f6b55d317f63efed82b6e0a58bb82 to your computer and use it in GitHub Desktop.
Save sthembi/b56f6b55d317f63efed82b6e0a58bb82 to your computer and use it in GitHub Desktop.
Wordpress Reverse Proxy Plugin
<?php
/**
* @package Reverse Proxy
*/
/*
Plugin Name: Reverse Proxy
Plugin URI: https://instrumentalapp.com/
Description: Reverse proxy setup for Instrumental blog
Version: 1.0
Author: James Paden
Author URI: https://instrumentalapp.com
*/
// Change to match the desired subfolder, no leading or tralling slash
// if there is no subfolder because using subdomains leave blank
define("RP_SUBFOLDER", "");
function rp_is_login_page() {
return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'));
}
if ( $_SERVER["REMOTE_ADDR"] != "127.0.0.1" && !is_admin() && !rp_is_login_page() && $_GET["preview"] != "true" ) {
add_action( 'init', function () {
if (!$_SERVER["HTTP_X_IS_REVERSE_PROXY"]) {
//not coming from us, 404 it.
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit;
}
});
//From http://stackoverflow.com/questions/772510/wordpress-filter-to-modify-final-html-output
ob_start();
add_action('shutdown', function() {
$final = '';
//$levels = count(ob_get_level());
//`$levels = ob_get_level();`the count() function is not needed and actually causes the buffer to fail. For more info, see link in comments to SO thread.
$levels = (ob_get_level());
for ( $i = 0; $i < $levels; $i++ ) {
$final .= ob_get_clean();
}
// Apply any filters to the final output
//$final = str_replace("http://" . $_SERVER["HTTP_HOST"], "https://" . $_SERVER["HTTP_X_ORIGINAL_HOST"] . "/" . RP_SUBFOLDER, $final);
//site has ssl and removed the "/"
$final = str_replace("https://" . $_SERVER["HTTP_HOST"], "https://" . $_SERVER["HTTP_X_ORIGINAL_HOST"] . RP_SUBFOLDER, $final);
echo $final;
}, 0);
}
# Example code for Apache Proxy config
# Put this inside the <VirtualHost> directive
# replace subfolder with "/" to allow for full subdomain to be proxied
# RequestHeader set Host was key to proxying to the correct site within the WP multisite
<Location />
RequestHeader set X-Is-Reverse-Proxy true
RequestHeader set X-Original-Host yourfinalwebsite.com
ProxyPass http://yourwordpresssitesubdomain.com
ProxyPassReverse http://yourwordpresssitesubdomain.com
RequestHeader set Host yourwordpresssitesubdomain.com
</Location>
# Example code for Nginx config
# Put this inside the "server {" section
# check nginx info (Google) to match Apache config to nginx proxy config
location /blog/ {
proxy_set_header X-Original-Host $host;
proxy_set_header X-Is-Reverse-Proxy "true";
proxy_pass_header Set-Cookie;
proxy_cookie_path / /blog/;
proxy_pass http://yourwordpresssitesubdomain.com/;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment