Compute Engine Google Cloud

How to Set up WordPress Reverse Proxy on Google Cloud

How to Set up WordPress Reverse Proxy on Google Cloud. Load your WordPress site in your subdirectory which is hosted on different server.

Let’s consider an example to better understand what a reverse proxy can be used for. Imagine that you have a non-WordPress website or a WordPress site which loads at yourdomainname.com and you wish to use another WordPress installation to power a blog  for your main site. You want that blog to load at yourdomainname.com/blog while hosting your blog with a separate Google Compute Engine Instance.

To make this work reverse proxy rules is configured on your main site instructing Nginx to send any requests for the /blog subdirectory to another web server hosting the /blog website.

PlatformRAMSSDCost
Alibaba Cloud512 MB20 GB$2.50/mo
Cloudways – Managed Cloud1 GB25 GB$10/mo
Kinsta – Google Cloud Managed WP1 Website10 GB$30/mo

Prerequisites

  1. Compute Engine Instance running for your main domain and your subdomain.
  2. For setting up Compute Engine, see the Setting up Compute Engine Instance.
  3. For installing Nginx and PHP 7.3, see how to install LEMP in Compute Engine Instance.
  4. Domain name is pointed to your virtual machine.
  5. For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
  6. Google Cloud SQL Setup, see Setup Cloud SQL and connect with Compute Engine.

Reverse Proxy – Subdirectory

Subdirectory Set up (https://yourdomainname.com/blog)

Setup a Compute Engine Instance and install WordPress and configure it as a subdomain (https://blog.yourdomainname.com) and install WordPress.

Changes in your subdomain instance

SSH to your subdomain instance and update the following changes

sudo nano /etc/nginx/sites-available/blog.yourdomainname.com

Remove the server blocks with port 80, because we are going to connect only using HTTPS.

Once done hit Ctrl + X followed by Y to save and exit

Validate your Nignx configuration and restart Nginx

sudo nginx -t
sudo service nginx restart

Now edit the wp-config.php file and define the URL settings.

sudo nano ~/blog.yourdomainname.com/public/wp-config.php

Add the below lines after define('DB_COLLATE', '');

 
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS']='on';

if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

$_SERVER['REQUEST_URI'] = "/blog".$_SERVER['REQUEST_URI'];

define('WP_HOME', 'https://www.yourdomainname.com/blog');
define('WP_SITEURL', 'http://www.yourdomainname.com/blog');

These are your changes made in your subdomain instance

Changes in your main domain instance

Now SSH to your main domain instance and add the following to your Nginx configurations.

sudo nano /etc/nginx/sites-available/yourdomainname.com

Add the following in your main server block

location ^~ /blog/ {
       rewrite ^/blog/(.*)$ /$1 break;

       proxy_set_header X-Real-IP  $remote_addr;
       proxy_set_header Host $host;
       proxy_pass https://blog.yourdomainname.com;

       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Proto $scheme;
   }

   location ^~ /blog/wp-includes {
       proxy_pass https://blog.yourdomainname.com/wp-includes;
   }

   location ^~ /blog/wp-login.php {
       proxy_pass https://blog.yourdomainname.com/wp-login.php;
   }

   location ^~ /blog/wp-admin {
       proxy_pass https://blog.yourdomainname.com/wp-admin;
   }

Once done hit Ctrl + X followed by Y to save and exit

Validate your Nignx configuration and restart Nginx

sudo nginx -t
sudo service nginx restart

Now visit your main domain with the /blog you will see your blog that is hosted on different server.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.