Node - Set virtual host on local computer - node.js

I'm trying to set virtual host on my local machine for my Node (Express) project. But I cant figure out how to avoid port number
This is what I had entered on my /etc/hosts file.
192.168.151.207 www.potato.com
192.168.151.207 www.tomato.com
I can access site by www.potato.com:3000 but I want it to be simply www.potato.com.
I was Googling for last few days but all most all the solution says to use Nginx for reverse proxy. I also read somewhere that if I use Nginx I can't use Socket. And socket is something which I have to use in next phase of the project.
Any help is heartily appreciated.

Did you try virtualhost npm package?
Make your HTTP server hostname-aware very simply.
You define the handler for each server name, and that will return the
final handler to be passed to your HTTP server.
Works fine with Express.
You only need to use nginx or any orther proxy solution (there are nodejs modules too you could integrate with your application) if you want serve each virtualhost with different applications (because they cannot listen to the same port).

Here the answer to my question. I use Nginx only and setup a reverse proxy.
First on my /etc/hosts file I add the domain which I want to use.
127.0.0.1 tomato.com
This means whenever I open this URL "tomato.com" browser will change for 127.0.0.1. But my Express server is running on 127.0.0.1:3000. Now we need to point 127.0.0.1 to 127.0.0.1:3000. Using Nginx we can configure this. Below given line of code does this. /etc/nginx/sites-available/tomato.conf
server_name tomato.com;
location / {
proxy_pass "http://127.0.0.1:3000/"
}
For more detail check this post from Digitalocean

Related

High Availability Multi Backup Server

I have a project which need to have multi-backup server. It is better to look at the below topology:
So, We will have 4 Remote Site Server which will act as Backup Server in case of Main server is down. In Normal condition Devices will connect to Remove Site Server IP and passthrough it to Mainserver. In case that Mainserver is down or Link from Remote Site Server to Main server is fail, then Remote Site Server will act as Mainserver and start serving services.
I know I can do this by using Nginx and use proxy_pass for tcp , but the thing that we have a dynamic port, for example.
The user can add port 4500 to server virtual server, and later add another port 45001 to be accessed by clients.
I'm now sure how to do it with Nginx.
Also, I have been looking for another solution like keepalived or pacemaker but seems like they are only having like Master-Backup mechanism, not master-backup,backup,backup
Any advice how to get this done?
Appreciate your ideas!
As far as I understood, You need some way to change servers dynamically like server:3000,server3001 and so on. If so you can save server list in text file and use revers proxy to use server form the serverfile.
after that you can simply update the serverlist file to update server from you code.
for example:
Create a file called servers.txt in a directory of your choice and list all the backend servers that you want to use in the following format:
server1.example.com
server2.example.com
server3.example.com
server3.example.com
In the Nginx configuration file, define an upstream block that refers to the servers.txt file and specifies the relevant proxy parameters, like this:
http {
upstream backend {
server unix:/var/run/php-fpm.sock;
include /path/to/servers.txt;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
}
}
}
specify additional parameters for other configuration.
finally you can change file servers.txt as per your need. But i dont know if you need to reload nginx server everytime you update your server.txt file.

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/

Hosting PHP and Node.js apps on the same server with multiple domains

I have a Linode VPS, currently running lighttpd to serve up my PHP websites and listening on port 80.
I'm also running Node.js, which listens on port 81, and uses websockets and HTTP to interact with the client.
There's a couple of different domains that I would like to point to this server. Ideally, I would like the domains which host the PHP sites to all talk to the same lighttpd server, and the sites which use node.js would somehow redirect to the port node.js is listening on unbeknownst to the client (e.g. no 30x redirect).
example-php1.com:80 -> linodebox:80 lighttpd /var/www/example1
example-php2.com:80 -> linodebox:80 lighttpd /var/www/example2
example-node.com:80 -> linodebox:81 node.js
Is there a way to do this, either by setting DNS entries or tweaking iptables? Does lighttpd need to be a proxy for node.js? The websockets feature needs to work without any fallbacks, and visiting a non node domain, e.g. example-php1.com:81, should not expose the node application.
I feel the perfect solution wouldn't require changes to existing application code nor require proxying between software web servers, but I could be wrong.
What's up Tom!?
I recommend HA-Proxy, it's one of the most high performance proxies out there and should accomplish what you're trying to do there.
I'm doing something similar with nginx acting as a proxy, it's easy but not the fastest.
HA-Proxy's website is here http://haproxy.1wt.eu
If you wanted a 'pure' solution, you could probably get the answer from looking at ha-proxy's source code. You can't really do it with iptables. Something has to read the HTTP header to determine where the request came from to route it locally.
I had basically the same problem and I ended up using node-http-proxy (also available in npm as http-proxy).
You just need a simple config file:
{
router: {
'example-php1.com': 'linodebox:80',
'example-php2.com': 'linodebox:80',
'example-node.com': 'linodebox:81
}
}
Then just run node-http-proxy --config options.json and you're set. If you want to run lighttpd and node on the same machine, you'll have to start lighttpd on a different port (I use 81 for php and 3000 for node - adjusting the config is easy). I also use forever to manage my node instances.
Ya'll are gonna hate me...
I ended up going with a second IP address, then followed the Linode tutorial to setup multiple static IPs. Then, I configured lighttpd to bind to one IP address and Node.js to bind to another IP address.
This isn't a great solution as it doesn't scale.
Update: lighttpd 1.4.46 (released back in 2017) added multiple ways to accept WebSocket connections:
lighttpd mod_wstunnel
lighttpd mod_proxy
lighttpd mod_cgi

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

How do IP addresses work on a VPS? Routing a domain name to Node.JS

This is an absolute newb question. But I'm buying my first VPS for the reason that I want to install and start creating applications in Node.JS.
I can't visualise in my mind how the server works and where all of the applications such as Apache, Node.JS and PHP sit. I'm so used to a GUI.
I want www.mydomain.com to point to node.JS on my server, let's say Node is listening to port 8080. Now I know that HTTP defaults to port 80 of the IP address, so I can't use that. How do I set the domain up to resolve at www.mydomain.com:8080 - I read this wasn't possible...
My brain is melting.
Thanks :)
You just point the domain to your ip address as you normally do. The issue you will have it that HTTP default to port 80, so either you manually add the port at the end of the host to get to the page or you setup Apache to proxy specific urls to 8080, which gets some of your Node stuff appearing to work under 80.
If you aren't using Apache for anything you can also have your Node app bind to port 80. You will probably need to setup authbind or something to give your node app permission to bind to port < 1024.

Resources