Apache 2.4 as a reverse proxy for WordPress

I know that Nginx is sometimes regarded to be the cool web server option while Apache is at times considered to be somewhat old, slow. I don’t necessarily share this perception. To me, an Apache instance that’s only using a small set of modules is comparable to nginx, at least ever since it started supporting event driven approach. But it’s true that I’m not an expert when it comes to high volume web traffic, I only run this little blog of mine where Apache has no issues to deliver.

Performance aside, I’m still more inclined to use Apache anyway because to me, it’s an embodiment of an opensource-governed project and as such, it seems to me more likely that it will continue to exist and be well maintained in the long term. That being said, I do not mean to belittle Nginx in any way, it’s a great piece of software. I simply prefer Apache more.

So even if it’s less common, I’m also using Apache as a reverse proxy occasionally. Like when there’s a WordPress instance running in a podman container in the background. But when I moved WordPress there originally, I ran into a couple of issues caused by the fact that client connection to Apache was HTTPS but the reverse proxy was talking to the container over plain HTTP. To remediate this I had to put the following at the top of wp-config.php:

if ( (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) || (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) { $_SERVER['HTTPS'] = 'on'; }

This basically checks “if either of those forwarded headers is present, this request came through the proxy” and so it manually informs WordPress that this was in fact a request coming through HTTPS.

Other that that, it works just fine. This is a typical proxy setup in case there is a container behind the proxy:

ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
ProxyPass /  http://localhost:8081/
ProxyPassReverse /  http://localhost:8081/

Sometimes, I have a different use case where the proxy is forwarding requests to a completely different host, if that is a case, it’s as simple as:

SSLProxyEngine on
ProxyPass "/" "<https://www.example.com/">
ProxyPassReverse "/" "<https://www.example.com/">