Scratch Pad
Published in

Scratch Pad

How to ReverseProxy Wordpress at “/blog” from any (sub-)domain

There is a common use case for a Wordpress blog, where it lives “inside” your website in a sub-directory: www.example.com/blog. With traditional server applications this is extremely easy to configure. Just install Wordpress at the subdirectory location. What if your web servers are stateless and leverage technologies like docker?

Here is the goal: make a blog appear “inside” of a web application that leverages docker at the /blog location.

What you will need:

Let’s do this: we have a website at https://www.example.com and want the blog to appear at https://www.example.com/blog but currently it is running at http://someblog.somedomain.com.

Enable proxy module for your server
If you are doing this on a server you will have to run this in super user mode and will most likely have to restart your server.

a2enmod proxy proxy_http ssl

I was doing this in docker so I added the command to my Dockerfile.

Configure Apache / Nginx ProxyPass & ProxyPassReverse
Amend your web server configuration file to include ProxyRequests, ProxyPass, and ProxyPassReverse.

<VirtualHost *>    DocumentRoot /var/www/app/public
SSLProxyEngine On

ProxyRequests off
ProxyPass /blog http://someblog.somedomain.com
ProxyPassReverse /blog http://someblog.somedomain.com
</VirtualHost>

Edit WordPress Config File
add the following code near the bottom of the wp-config.php file.

# ProxyPass Settings 
#
# DO NOT REMOVE: overriding the following variables is
# required to ensure that any request /blog/* is handled
$_SERVER[‘REQUEST_URI’] = ‘/blog’ . $_SERVER[‘REQUEST_URI’];
$_SERVER[‘SCRIPT_NAME’] = ‘/blog’ . $_SERVER[‘SCRIPT_NAME’];
$_SERVER[‘PHP_SELF’] = ‘/blog’ . $_SERVER[‘PHP_SELF’];

This will prevent Wordpress from redirecting you are using nested urls for permanent links.

Update July 13th 2021: You may also see define(...) statements for WP_SITEURL and WP_HOME.If you do see them, make sure to update the definition to your desired URL as well:

define('WP_SITEURL', 'ttps://www.example.com/blog');
define('WP_HOME', 'https://www.example.com/blog');

Edit Wordpress database configuration values

UPDATE wp_options SET option_value = 'https://www.example.com/blog' WHERE option_name IN('siteurl', 'home');

That should do it!
You should be able to go to https://www.example.com/blog and browse, edit, and manage you Wordpress instance like you are used to.

Update April 28th, 2018: A note for high traffic websites
If you are using a hosting service like WPEngine to host your blog instance, you may run into a random occurrence of 503 errors for the proxied pages. This can be caused by DDoS protection systems of the hosting provider. The reverse proxy causes all your traffic to appear from the limited number of IP addresses on your web server(s). Example: if you have 3 servers, all traffic will appear as coming from those 3 IP addresses. Work with your hosting provider to whitelist your IPs if they are static. If you have a scaling cluster, a better solution is to whitelist your IP range (CIDR blocks) instead.

--

--

software projects, snippets, and solutions that took too long the first time around…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store