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

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
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