Roots Discourse

Proxy a separate domain to a single page

I’m currently using Trellis to serve a WP instance on domain1.com. I want domain2.com to proxy to a single page on domain1.com/page, so without any redirections. I’ve found a WP plugin that’s supposed to do this:

But I have trouble instructing Trellis to achieve this. I’ve come up with the following definitions in wordpress_sites.yml:

wordpress_sites:
  domain1.com:
    site_hosts:
      - canonical: domain1.com
        redirects:
          - www.domain1.com
      - canonical: domain2.com
        redirects:
          - www.domain2.com

This correctly links the complete domain2.com to domain1.com, without any redirections. I can access the same content on both sites. This is bad for SEO though, and I want to only use domain2.com for a single domain1.com/page.

I’ve tried using the plugin above in combination with this, but that doesn’t seem to have any effect.

I’ve also tried to disable the plugin and use NGINX config instead. I’ve put this file into the folder nginx-includes/domain2.com:

server {
	listen 80;
	listen [::]:80;
	server_name domain2.com;

	location / {
		proxy_redirect off;
		proxy_set_header Host $http_host;
		proxy_set_header X-Forwarded-Host $http_host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

		proxy_pass https://domain1.com/page/;
	}
}

This doesn’t seem to do anything either.

I’ve seen separate topics on this forum asking for the same setup, but without any solutions.

It looks like this isn’t possible to achieve with Trellis alone. Any ideas?

2 Likes

Doesn’t the canonical link in the head take care of the double content SEO problem? With Yoast I think you can set the canonical url per page.

The nginx-includes folder should match your site name in wordpress_sites, in this case domain1.com, so that explains the not working redirect

Indeed, it looks like Yoast handles that by setting the default canonical url to the WP permalink, so the duplicate content is OK.

I’ve tried putting the NGINX redirect under domain1.com before, but then it complains about the server directive not being allowed in that place. But I do think I need to listen to the server directive, as I only need to proxy for server_name domain2.com

Then you need to make a child template file if you want to use the server block: https://roots.io/trellis/docs/nginx-includes/

1 Like

Yeah, that’s a good pointer. I’ve dived deep into nginx-includes and have something half-working. But I’m also running into bugs causing double request headers, and unreliable requests (every other request fails). Either I just don’t understand it or Trellis isn’t made for this thing. It feels like I’m going against the principles of Trellis.

Nobody on this forum seems to have made it work yet: pointing a single domain to a page on another domain. Maybe this is just too complicated for Trellis?

This is a tricky one indeed…

In order to have a valid SSL certificate for the domain to proxy, it needs to stay as canonical in the site_hosts.

However that will cause the domain to be added as server_name in the main server block in the roles/wordpress-setup/wordpress-site.conf.j2 template, making it impossible to add a location block, just for that domain.

So what I did is overriding the server_id block and exclude the proxy domain from the site_hosts_canonical array:

{% block server_id -%}
  listen {{ ssl_enabled | ternary('[::]:443 ssl http2', '[::]:80') }};
  listen {{ ssl_enabled | ternary('443 ssl http2', '80') }};
  {% if env == 'development' -%}
  server_name {{ site_hosts_canonical | union(multisite_subdomains_wildcards) | difference('proxy-domain.test') | join(' ') }};
  {% else -%}
  server_name {{ site_hosts_canonical | union(multisite_subdomains_wildcards) | difference('proxy-domain.com') | join(' ') }};
  {% endif -%}
{% endblock %}

and then add the conf for the proxy domain in the server_before block:

{% block server_before %}
server {
  listen {{ ssl_enabled | ternary('[::]:443 ssl http2', '[::]:80') }};
  listen {{ ssl_enabled | ternary('443 ssl http2', '80') }};
  {% if env == 'development' -%}
  server_name proxy-domain.test;
  {% else -%}
  server_name proxy-domain.com;
  {% endif -%}

  ... 

  location / {
    {% if env == 'development' -%}
    proxy_set_header Host domain.test;
    {% else -%}
    proxy_set_header Host domain.com;
    {% endif -%}
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_cache_bypass $http_upgrade;
    proxy_pass_request_headers on;
    proxy_redirect off;
    proxy_http_version 1.1;

    {% if env == 'development' -%}
    proxy_pass http://domain.test/proxy-domain-page/;
    {% else -%}
    proxy_pass https://domain.com/proxy-domain-page/;
    {% endif -%}
  }
{% endblock %}

And although this does work, it seems it’s not consistent.

Sometimes it loads the page in one go, but sometimes the browser hangs on:

Waiting for proxy-domain.com

and takes a really long time to load.
There are no relevant errors in /srv/www/domain.com/logs/error.log?

When I move the proxy domain conf to the redirects_domains block I get the same performance issues…

Does anyone know a better solution?
Thanks!

1 Like

Wow, never knew this could cause an issue like this, but I moved the site from a TransIP VPS to a Digital Ocean Droplet and upgraded to Ubuntu 18.04 right away and poof…

problem gone! :open_mouth:
weird

This topic was automatically closed after 42 days. New replies are no longer allowed.