[wp-hackers] Make WP more usable behind load balancers/proxies

Nikola Nikolov nikolov.tmw at gmail.com
Tue Oct 15 17:59:54 UTC 2013


Why don't you just create an mu-plugin that will override the
$_SERVER['HTTP_HOST'] variable?

Simply do something like

if ( isset( $_SERVER['HTTP_X_FORWARDED_HOST'] )
&& $_SERVER['HTTP_X_FORWARDED_HOST'] && _is_valid_host(
$_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
    $_SERVER['HTT_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

Note that the "_is_valid_host()" function is not an actual function. You
will have to define a function that will check the value passed to it and
make sure that it's acceptable(for instance you can check it against an
array of your domains).


On Tue, Oct 15, 2013 at 8:52 PM, Hauke <mailman at haukebruno.de> wrote:

> Hi there,
>
> is it possible to change code lines like
>
> $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] .
> $_SERVER['REQUEST_URI'] );
>
> into something like this:
>
> if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
>   $serverhost = $_SERVER['HTTP_X_FORWARDED_HOST'];
> } else {
>    $serverhost = 'http://' . $_SERVER['HTTP_HOST'];
> }
> $current_url = set_url_scheme( $serverhost . $_SERVER['REQUEST_URI'] );
>
> In my case I changed that in wp-admin/includes/class-wp-list-table.php
> (lines 494 and 651, WP 3.5.1 (same in WP 3.6.1).
>
> I am not a developer, therefore that code isn't that nice. It's more like
> an
> idea for you guys with a lot more knowledge than mine.
>
> The reason why I need that code:
>
> We use WP (and some other apps) under one domain, e.g. example.com.
> In our case, we want to use (pseudo) subfolder domains for that different
> apps, e.g. 'example.com/blog', 'example.com/shop' or 'example.com/foo'. At
> the same time, we want to seperate these apps in one VHost per app (we
> use Apache + fcgid for separating PHP too).
> So we put a load balancer/caching proxy (Varnish) in front of our
> webservers, which handles those URL rewriting stuff. It detects
> example.com/blog and rewrites the HTTP HOST Header Field to something
> internal like 'blog.example.local'. The Requests now reach the correct
> VHosts on the backend webservers. We provide the original hostname in
> the HTTP X_FORWARDED_HOST Header Field.
> WP works good at all with that setting, but there are some things like
> pagination or that list table one the 'All Pages' site in the WP backend,
> that doesn't work because of producing URIs with the internal hostname.
>
> --
> Best regards,
> Hauke
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list