Skip to content

Instantly share code, notes, and snippets.

@ajmorris
Forked from JamesPaden/reverse-proxy.php
Last active August 8, 2017 19:17
Show Gist options
  • Save ajmorris/c46dae2ff37e8b3b4b39c59dbf66805b to your computer and use it in GitHub Desktop.
Save ajmorris/c46dae2ff37e8b3b4b39c59dbf66805b to your computer and use it in GitHub Desktop.

Revisions

  1. ajmorris revised this gist Aug 8, 2017. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions reverse-proxy.php
    Original file line number Diff line number Diff line change
    @@ -41,4 +41,20 @@ function rp_is_login_page() {
    $final = str_replace("http://" . $_SERVER["HTTP_HOST"], "https://" . $_SERVER["HTTP_X_ORIGINAL_HOST"] . "/" . RP_SUBFOLDER, $final);
    echo $final;
    }, 0);
    }

    //This would only be needed if you are using Yoast SEO
    add_filter( 'wpseo_canonical', 'lc_persistent_canonical' );

    /**
    * Make this a universal change since we want this applied everywhere
    * even if we aren't viewing view Reverse Proxy.
    *
    * Replace the "final_url" with the correct Final URL for the site
    *
    * @param $canonical string
    */
    function lc_persistent_canonical( $canonical ) {
    $canonical = str_replace( site_url(), RP_FINAL_URL . RP_SUBFOLDER . '/', $canonical );
    return $canonical;
    }
  2. @JamesPaden JamesPaden revised this gist Dec 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reverse-proxy.php
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@


    // Change to match the desired subfolder, no leading or tralling slash
    RP_SUBFOLDER = "blog";
    define("RP_SUBFOLDER", "blog");

    function rp_is_login_page() {
    return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'));
  3. @JamesPaden JamesPaden revised this gist Sep 3, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions z-nginx-config.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # 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/;
    }
  4. @JamesPaden JamesPaden renamed this gist Sep 3, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @JamesPaden JamesPaden renamed this gist Sep 3, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @JamesPaden JamesPaden revised this gist Sep 3, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # 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>
  7. @JamesPaden JamesPaden revised this gist Sep 3, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions reverse-proxy.php
    Original file line number Diff line number Diff line change
    @@ -13,13 +13,13 @@


    // Change to match the desired subfolder, no leading or tralling slash
    INSTRUMENTAL_SUBFOLDER = "blog";
    RP_SUBFOLDER = "blog";

    function instrumental_is_login_page() {
    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() && !instrumental_is_login_page() && $_GET["preview"] != "true" ) {
    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.
    @@ -38,7 +38,7 @@ function instrumental_is_login_page() {
    }

    // Apply any filters to the final output
    $final = str_replace("http://" . $_SERVER["HTTP_HOST"], "https://" . $_SERVER["HTTP_X_ORIGINAL_HOST"] . "/" . INSTRUMENTAL_SUBFOLDER, $final);
    $final = str_replace("http://" . $_SERVER["HTTP_HOST"], "https://" . $_SERVER["HTTP_X_ORIGINAL_HOST"] . "/" . RP_SUBFOLDER, $final);
    echo $final;
    }, 0);
    }
  8. @JamesPaden JamesPaden created this gist Aug 24, 2015.
    44 changes: 44 additions & 0 deletions reverse-proxy.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    <?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
    INSTRUMENTAL_SUBFOLDER = "blog";

    function instrumental_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() && !instrumental_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"] . "/" . INSTRUMENTAL_SUBFOLDER, $final);
    echo $final;
    }, 0);
    }