Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
define("RP_SUBFOLDER", "blog");
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());
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);
echo $final;
}, 0);
}
# Example code for Apache config
# Put this inside the <VirtualHost> directive
<Location /blog>
RequestHeader set X-Is-Reverse=Proxy true
RequestHeader set X-Original-Host yourwebsite.com
ProxyPass http://yourpressableblog.com
ProxyPassReverse http://yourpressableblog.com
</Location>
# Example code for Nginx config
# Put this inside the "server {" section
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://yourpressableblog.com/;
}
@commandantp

This comment has been minimized.

Copy link

@commandantp commandantp commented Feb 26, 2016

Hi James,
Awesome work that you shared.

--- EDIT ----
In fact if your site is not in HTTPS remove the s line 41 of the plugin and keep the blog.example.com as the WP_HOME & WP_SITEURL. That should solve the problem.
Which means all your admin panels keeps the default url blog.example.com when showing links. Make sure you watch out for this.
The previous blog posts links blog.example.com/123 won't get redirected. Also if you have your site in https and your media are hosted with your wp blog which is not https it won't load the resources.

@james correct me if I'm wrong thanks.

@chitikenasatish

This comment has been minimized.

Copy link

@chitikenasatish chitikenasatish commented Oct 17, 2019

Hi , This is good and exactly what I need . Can you please let me know how to add reverproxy.php in the code or include . I am new to wordpress . Thanks a lot for the response.

@JamesPaden

This comment has been minimized.

Copy link
Owner Author

@JamesPaden JamesPaden commented Oct 17, 2019

HI, I cannot recommend the usage of this if you aren't not an expert Wordpress user. It's an advanced technique and will likely require ongoing maintenance and changes.

@chitikenasatish

This comment has been minimized.

Copy link

@chitikenasatish chitikenasatish commented Oct 17, 2019

Thanks for quick reply . Please let me know upgrade challenges . If you have any document please share . Also I tried the solution with minimal change it worked . I also like to understand better about this code before taking decision. Thanks again helping on this issue

@ouadie-limouni

This comment has been minimized.

Copy link

@ouadie-limouni ouadie-limouni commented Dec 23, 2020

Hi James, I just stumbled upon your code which seems to align with what I'm trying to achieve.
I have 2 questions if you don't mind:
1- do you change the site_url and home_url on the origin site to be "finalsite.com/blog" or do you leave those as originsite.com ?
2- how do you get assets from wp-content to not throw 404 errors when accessed via the RP URL (finalsite.com/blog/wp-content/...) since they physically live on originsite.com/wp-content/...

Thank you,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment