0

I'm trying to implement Nginx as a reverse proxy to serve different web apps via subdomains and can't seem to find any beginner friendly guides on how to do so.

Example:

  • cloud.mydomain.com => [local] 192.168.0.33:5000;
  • web.mydomain.com => [local] 192.168.0.33:5001;

The idea is to open port 80 and 443 on my router and then forward all traffic towards the reverse proxy which then talks to the respective server (specified via the subdomain configs) on my LAN and passes the request back to the client.

What I've tried so far (without any luck though):

1) Editing the default file in /etc/nginx/sites-available/ to look somewhat like this:

server {
    listen 80;
    server_name cloud.mydomain.com;
    location / {
        proxy_pass http://localhost:5000/;
    }
}

server {
    listen 80;
    server_name node2.mydomain.com;
    location / {
        proxy_pass http://localhost:5001/;
    }
}

This would result in ANY subdomains pointing to whatever I specified in the first block :(

2) Unlinking the default file, creating a new test.conf file under /etc/nginx/sites-available/ and linking it to /etc/nginx/sites-enabled/ (nginx -t didnt report any errors). The config was same as above.

Setup: I'm running Ubuntu Server 19.10. I do a domain name and I've already setup the subdomains as CNAME records.

Note: I do not want to use Nginx as a web server. I'm trying to use it for node.js web apps, DBs, my nextcloud etc.

Note 2: I only care about subdomains right now. Yes, I will need to add SSL configs and such but once I understand how to do proper configs I'll be able to figure all that out using the docs (I hope? lol).

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
lacamera
  • 13
  • 1
  • 4
  • You are close (and both your approaches are effectively the same). Instead of using `localhost` though, you need to use some internal label that you have defined with a corresponding `upstream`- like say `proxy_pass http://cloud;`. [Here's an example](https://stackoverflow.com/questions/5877929/what-does-upstream-mean-in-nginx). Use a different `upstream` server for each of your services. – Don't Panic Jan 14 '20 at 10:57
  • Thanks a bunch, I'll try it once I'm back home ;) – lacamera Jan 14 '20 at 11:00
  • In fact if you don't want to use nginx as your server maybe you can skip the `upstream` and just do `proxy_pass http://192.168.0.33:5000`, though I'm not sure. There are plenty of similar questions here on SO, now you know the terms try some searching. – Don't Panic Jan 14 '20 at 11:03

0 Answers0