Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeyBurzynski/34229834af1ac7a7e1e5007bc5b17021 to your computer and use it in GitHub Desktop.
Save JoeyBurzynski/34229834af1ac7a7e1e5007bc5b17021 to your computer and use it in GitHub Desktop.
Tutorial: How to Configure a Reverse Proxy for Use with WPEngine/Wordpress Sites [2022]

Configuration: How to Configure a Reverse Proxy for Use with WPEngine/Wordpress Sites [2022]

Configure an external proxy from domain.com to install1.wpengine.com.

Make sure you send the Cache-Control $http_cache_control header in your requests to WP Engine. If this isn’t configured you will be permanently logged into /wp-admin and it will not auto logout. That can be a serious security concern.

Make sure you pass the X-Forwarded-For headers to WP Engine so that we see the actual IPs and not the proxy IP. If this isn’t in place, you will get blocked by WP Engine’s firewall.

Gutenberg requires the Rest API so if you are using it, you will also need to proxy a header with a browser request to keep Gutenberg functioning. Add the following code into the wp-config.php file only on the first environment. Make sure to update example.com with the new live domain.

if ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
       $_SERVER['HTTP_HOST'] = 'example.com';
}


define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);

Reach out to WP Engine Support to add the following NGINX rule into the Nginx configuration for this particular website. Replace 0.0.0.0/0 with the actual IP range of the proxy provider.

real_ip_header X-Forwarded-For;
set_real_ip_from 0.0.0.0/0;

Add the following code into the site’s wp-config.php file for the second environment, making sure to update the domain.com and domain.com/blog details to your domain and subdirectory.

if ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
       $_SERVER['HTTP_HOST'] = 'domain.com';
}


define( 'WP_HOME', 'https://domain.com/blog' );
define('WP_SITEURL', 'https://domain.com/blog') ;

Install the mu-plugin:

RequestReach out to WP Engine support to add the following Nginx rule on the environment.

Replace 0.0.0.0/0 with the actual IP range of the proxy provider.

real_ip_header X-Forwarded-For;
set_real_ip_from 0.0.0.0/0;

Finally, run a search and replace on the website to find the WP Engine CNAME (EX: install.wpengine.com) and replace it with your subdirectory (EX: domain.com/blog).

<?php /**
* Stop htaccess rewrites
*
* @package wpengine-stop-htaccess-rewrites
* @author wpengine
* @license Proprietary
*
* @wordpress-muplugin
* Plugin Name: Stop htaccess rewrites
* Plugin URI: https://wpengine.com
* Description: Stops Rewrite Flush from changing the htaccess file on proxy passed sites.
* Version: 0.1.0
* Author: wpengine
* Author URI: https://wpengine.com
* Text Domain: wpengine-stop-htaccess-rewrites
* License: Proprietary
*/
namespace WPEngine\StopHardRewrite;
add_action("plugins_loaded", function () {
// Get the PATH from the site url.
$path = wp_parse_url(get_option("siteurl"), PHP_URL_PATH);
// If we have a path, then this is a proxy pass. Stop hard rewrites.
if (!empty($path) && "/" !== $path) {
add_filter("flush_rewrite_rules_hard", "__return_false");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment