routing subdomains to a specific port in nodejs - node.js

I have two react frontend servers (example1 and example2) listening on two ports on MyDomain.com:
MyDomain.com:4000 accesses example1
MyDomain.com:5000 accesses example2
I have two subdomains, example1.MyDomain.com example2.MyDomain.com
I want to create a third nodejs server that will listen on port say 3000 and route requests to ports 4000 and 5000 depending on the subdomain accessed.
example1.MyDomain.com will be routed to MyDomain.com:4000
and
example2.MyDomain.com will be routed to MyDomain.com:5000
could not find an example that addresses port numbers except for ngnix
I also tried using proxy middleware but the examples show how to route according to the suffix after the .com and that is not what I need.
Thanks for any help,
Amnon

I highly recommend you to use one of the popular proxies/load balancers for that Nginx, HAProxy, Envoy, Traefik.
If you want to do it specifically with node.js you will need create proxy server on your own,but that is not a good idea because the apps mention above already have that functionality and they are built specifically for that use case

Related

Is there a way to "host" an existing web service on port X as a network path of another web service on port 80?

What I'm trying to do is create an access website for my own services that run on my linux server at home.
The services I'm using are accessible through <my_domain>:<respective_port_num>.
For example there's a plex instance which is listening on port X and transmission-remote (a torrenting client) listening on port Y and another custom processing service on port Z
I've created a simple website using python flask which I can access remotely which redirects paths to ports (so <my_domain>/plex turns into <my_domain>:X), is there a way to display these services on the network paths I've assigned to them so I don't need to open ports for each service? I want to be able to channel an existing service on :X to <my_domain>/plex without having to modify it, I'm sure it's possible.
I have a bit of a hard time to understand your question.
You certainly can use e.g. nginx as a reverse proxy in front of your web application, listen to any port and then redirect it to the upstream application on any port - e.g. your Flask application.
Let's say, my domain is example.com.
I then can configure e.g. nginx to listen on port 80 (and 443 for SSL), and then proxy all requests to e.g. port 8000, where Flask is running locally.
Yes, this is called using nginx as a reverse proxy. It is well documented on the internet and even the official docs. Your nginx.conf would have something like:
location /my/flask/app/ {
# Assuming your flask app is at localhost:8000
proxy_pass http://localhost:8000;
}
From user's perspective, they will be connecting to your.nginx.server.com/my/flask/app/. But behind the scenes nginx will actually forward the request to your app, and serve its response back to the user.
You can deploy nginx as a Docker container, I recommend doing this as it will keep the local files and configs separate from your own work and make it easier for you to fiddle with it as you learn. Keep in mind that nginx is only HTTP though. You can't use it to proxy things like SSH or arbitrary protocols (not without a lot of hassle anyway). If the services generate their own URLs, you might also need to configure them to anticipate the nginx redirects.
BTW, usually flask is not served directly to the internet, but instead nginx talks to something like Gunicorn to handle various network related concerns: https://vsupalov.com/what-is-gunicorn/

Two local node servers and proxying traffic based on specified path

Im looking for a way to do the following:
Given two servers running locally on different ports:
localhost:3001
localhost:3002
I would like a third server running on port 3000 to route all traffic to localhost:3001 except for specific whitelisted paths. I would like a configuration file to specify the paths. For example.
* localhost:3001
/example localhost:3002
In this case all traffic is proxying to 3001, except for the route /example which will proxy to localhost:3002/example.
I do not want a redirect off of 3000 for any requests. I would like this intermediary server to appear to the only site. So I believe I would like the server running on port 3000 to be a proxy to the other two.
I am interested in doing this via nginx or a node.js / npm module if available. Is this possible? What is a simple way of doing it?
Nginx should suite perfectly for this job.
Usually it is used as a web tier for application servers located on other machines, but technically there's nothing different there from using the same host with different ports.
you should simply define localhost:3002 and localhost:3001 as the proxy_paths in your nginx configuration.
If you're not familiar with nginx, this can be a good place to start:
https://www.nginx.com/resources/wiki/start/

Running NodeJS/Express projects on production server

1 With different apps different website domain etc, NodeJS cannot go to production with host:*some port other than 80*, right? If I am wrong, how to deal with NodeJS apps with multiple website on the same machine? ( there is no virtualhost in NodeJS/Express server, isn't there?)
2 So the solution to go prod to me, only alternative is to use some proxy forwarding requests to the NodeJS/Express server IP:port, isn't it? If yes and if it is a different server ( proxy and NodeJS), what does express to start and listen to? (Say, server.listen('port', '0.0.0.0') or server.listen('port', '::')?
3 Any other alternatives to go production with NodeJS/Express projects?
You can use 80 but with sudo. However, it's not recommended.
You're right you need a proxy (nginx, haproxy, etc..) to sit in front of your Node.js app in order to use port 80.
I think you can omit host from server.listen so it will accept connection from ::.
NGINX is the best option to do, what you expect and see the NGINX documentation in official web site.

Node/Express respond only to specific domains

Is it possible to bind different hostnames to different Express instances?
For example: example.com would direct to one NodeJs/Express instance running on port 80 and example.org would direct to another NodeJs/Express instance running on port 80.
If this is completely confusing, think of how IIS is capable of binding specific host headers to specific IIS websites.

deploying a node.js on a new domain

I have a server that runs different websites on different ports. All of them (but one) are Apache servers and thanks to webmin, I managed to have, for instance, example.com point to 123.123.123.123:80 and example.fr to 123.123.123.123:8000, somehow automatically
I am now running a nodejs server on the same machine, so the 80, 8000, and many other ports are already taken. My nodejs listens on 8008. I have another domain name, say example.org, and I want it to point to my nodejs website, but I simply don't know how to do that! I have updated the DNS and everything is pointing to 123.123.123.123 (my server's IP). I want to avoid using an ugly example.org:8008/ for everything on this node server. How can I make it point implicitly to the 8008 port?? I must add that I cannot afford to take down the apache servers ;)
DNS only provides name to ip address mapping. It cannot handle ports. What you can do instead is to set up a proxy server listening on port 80. The proxy server can then return data based on the host header.
Your best option is to just redirect the request from Apache. Otherwise you can use a reverse proxy like Nginx. Also, you can write a lightweight proxy in node... check out this page

Resources