0

I want to serve a wordpress NGINX backend everytime the user visits www.domain.tld/blog. I know I can use different backends with Varnish like this:

sub vcl_recv {
    if (req.url ~ "^/blog/") {
        set req.backend_hint = wordpress;
    } else {
        set req.backend_hint = default;
    }
}

I know I could use a separate location ^~ /blog {} in NGINX, but this has the following disadvantage: If I use www.domain.tld/blog in the wordpress WP_HOME and WP_SITEURL variables some plugins do not work correctly. So I need to deliver wordpress without the trailing /blog at the end as a stand alone vhost.

So, what I actually want to achieve is:

User -> www.domain.tld/blog (Varnish) -> blog.domain.tld (Wordpress NGINX)

The main thing is that the content on blog.domain.tld/some-post should be displayed as www.domain.tld/blog/some-post. But, blog.domain.tld blog should not be accessible publicly as this would mean duplicate content.

So, how could I make blog.domain.tld/some-post "resolve" to www.domain.tld/blog/some-post with Varnish and NGINX and simultaneously making blog.domain.tld not world accessible?

I hope this was understandable :)

P.S.: It's not necessary for me to use blog.domain.tld - this is just an example, it could also be a 127.0.0.1:8008 vhost, doesn't matter.

  • 1
    This is a horribly bad idea. You'll need a lot more than simple Varnish (or even nginx!) tricks to make this "work". You'll probably even have to hack the WordPress core. A lot. Just stick with blog.example.com. – Michael Hampton Sep 4 '18 at 12:14
  • Thank you. Yes, I agree, using only blog.example.com is much easier, but not so SEO friendly as a subdirectory. Either I'll go without some addons or loose some SEO points. – chevallier Sep 4 '18 at 12:19
  • Who said it wasn't as SEO friendly? I've heard that before, but AFAIK there's actually no significant difference. – Michael Hampton Sep 4 '18 at 12:20
  • I read not just ones, that Google treats different hostnames as different hosts/sites. So www.mydomain.tld will not benefit as much from good SEO posts on blog.mydomain.tld as if it was in a subdirectory AFAIK. – chevallier Sep 4 '18 at 12:23
  • Yes, but they are in the same domain, and that's what counts. It's a lot more important to publish good content. – Michael Hampton Sep 4 '18 at 12:25
0

The following piece in vcl_recv will provide what you want by rewriting the URL that the backend will see:

if (req.http.host == 'domain.tld' || req.http.host == 'www.domain.tld') {
  set req.url = regsub(req.url, "^/blog/", "/");
} 

However I don’t understand what are those plugins and why they should be tolerated :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.