Skip to content

How to setup WordPress behind a reverse proxy within Laravel on RunCloud

Recently I ran into the issue of needing to move my WordPress install out of my Laravel codebase, because it was making deployment issues difficult and didn’t seem like the right way of doing things.

While you can move to a subdomain, doing so can allegedly have an SEO impact (and/or break old links). For this reason I decided to instead setup my Laravel site to reverse-proxy the /public/blog/ directory to a separate virtualhost — thus reducing the Laravel repository size and improving my continuous integration/deployment setup.

In this article I will explain how you can also do this. While this article also primarily focuses on Laravel and RunCloud, you can adapt it as required elsewhere.

Nginx configuration

RunCloud supports custom nginx configuration via their own custom includes. In this article we will require one custom config in for your main (Laravel) domain.

RunCloud -> Web App (yourdomain.com) -> Nginx Configuration -> location.main-before.blog.conf

location ^~ /blog/ {
rewrite ^/blog/(.*)$ /$1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_pass https://yourdomain.com/blog/;
proxy_redirect off;
}

Creating proxy domain

You can go ahead and create a separate WordPress proxy domain/app within RunCloud. You can either use a test domain, or optionally you can use your own subdomain. In this tutorial I will assume you have used blog.yourdomain.com. (Note: You can restrict it to only allow the upstream proxy later on)

Once you have created the new domain, you can move your WordPress blog from the Laravel app (e.g. /public/blog/*) and place it in /blog/ of the new private proxy subdomain/app.

wp-config.php

The only changes we need to make on the WordPress side are with wp-config.php

Ensure both WP_HOME and WP_SITEURL use yourdomain.com/blog:

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

Add HTTP_X_FORWARDED_HOST and HTTP_X_FORWARDED_FOR configuration:

/** 
Reverse Proxy configuration
Configures WordPress to use correct hostname and show correct source IP addresses when it's behind a reverse proxy
*/
if (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $list[0];
}

Optional: Securing proxy/private domain

If you want to also block access to the private proxy domain/ip, you can do so within the Nginx configuration for the proxy/private domain.

RunCloud -> Web App (blog.yourdomain.com) -> Nginx Configuration -> location.root.block.conf

allow 127.0.0.1;
deny all;
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x