0

I've configured before the following where I have 2 different apps under the same server but with different sub-domains. Something like:

  • domain.com points to ip1:3000 under root /var/www/html/site1
  • sub.domain.compoints to ip1:3001 under /var/www/html/site2

What I'd like to do now, for SEO reasons, is to have all apps under the same domain. But in order to keep my resource usage low and to distribute a bit the load, I'd like for the apps to be on different machines/servers/ips. So what I imagine is the following:

  • domain.com points to ip1:3000 under root /var/www/html/site1
  • domain.com/site2 points to ip2:3000 root /var/www/html/site2

How can I achieve this with NGINX? What do I need? Is it even possible?

I've looked a while for this and mostly what I find is the first case, but that's unrelated.

Any help would be appreciated. Thanks!

EDIT: To clarify, wouldn't this work?

server {
  server_name domain.com;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://localhost:3000;
  }

  location /app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://some.other.external.ip:3000;
  }
}
fillipvt
  • 181
  • 1
  • 1
  • 9
  • You can't do it that way. Hostnames resolve to IP addresses, URLs do not. – Michael Hampton Nov 05 '18 at 15:25
  • Thanks for the comment @MichaelHampton . I edited my question. Wouldn't something like that work? – fillipvt Nov 05 '18 at 16:49
  • 2
    Yes, you could do that, and in fact that is the reverse proxy setup that jouhisorsa recommended in his answer. but in that case the DNS would have the same IP address for both names. The other IP address would not be publicly visible. On another note, the supposed SEO benefits of a subdirectory over a subdomain are dubious at best. It's not like Google will fail to notice that these are two separate and distinct apps. You'll get more SEO mileage out of ensuring that the content is as good as possible. – Michael Hampton Nov 05 '18 at 17:02

2 Answers2

2

Domain names always point to specific servers, which you determine in your DNS records (like A, AAAA and CNAME records). You can have multiple servers serving the same domain name, but they are all expected to act the same way.

What you need is a reverse proxy. It means you have server(s) that forward the client's request to app servers. That way you can add as many app servers as you'd like. You can find more about reverse proxies on this NGINX's guide.

What you have on your edit's code looks exactly like a reverse proxy, way to go!

Akseli
  • 23
  • 5
  • I guess I should've worded my question differently. Sorry for the confusion! But in the end what I'd like to achieve is having two different apps in different servers each, being served under the same domain. Will look more into *reverse proxies* Thank you! – fillipvt Nov 05 '18 at 16:56
0

Managed to do this by implementing inside the NGINX conf the following:

location /app/ {
    rewrite ^/app(/.*)$ $1 break;
    proxy_pass http://some.other.external.ip;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_redirect off;
  }

This was done in guidance by this link and by the docs as suggested by @jouhisorsa.

I asked this question without knowing about proxies, but now with a little more knowledge about it, I'm sure that the correct term I should've used for this question and for Google were "proxy_pass nginx to another server", which gives you a fair amount of results, such as:

  1. Nginx: How to proxy_pass a subdirectory to another subdirectory
  2. How to config nginx proxy_pass to another proxy Server?
  3. nginx proxy_pass to another server/path/
  4. etc.

Which I believe are pretty related and give you some insight (even though some of those don't have an answer yet). Anyways, great knowledge there. Thanks for the help.

fillipvt
  • 181
  • 1
  • 1
  • 9