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.
Prerequisites
- Compute Engine Instance running for your main domain and your subdomain.
- For setting up Compute Engine, see the Setting up Compute Engine Instance.
- For installing Nginx and PHP 7.3, see how to install LEMP in Compute Engine Instance.
- Domain name is pointed to your virtual machine.
- For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
- 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.
Leave a Reply
You must be logged in to post a comment.