How to temporarely resolve a name to a localhost port? - linux

I develop websites with rails, and I was looking for a simple way to setup a dynamic name resolution for my app. The final product I need is to is: Every time I start a rails application (by typing rails server on my application folder, I want to run it on a random port and if I type the application name on my browser (like myapp.dev or something) it resolves to the localhost on the correct port.
The part of getting the app name and generating a random port is not the problem. The problem is how to resolve a name to a local port. Is there any simple tool on linux that allows me to do this?
Right now, the best I can think off is start a daemon that keeps track of when a rails app is started, annotate the port, and add an entry to itself in /etc/host with the app name. Then, whenever it receives a request, it forward to the correct app based on the name.
I can't believe this is the best way so ideas are highly appreciated.

I'm not sure if I understand your problem correctly, but DNS it not about ports. By using DNS you can resolve the name to IP not port or from IP to name (RevDNS). What you would do in your case is during start up of application on random port, forward another port which will be always the same.
For instance:
By iptables forward port 80 to random port of your application. Then you will always get to application by port 80.
Edit: I couldn't paste it in the comment because it's too long, so I give you answer here:
You can create a lot of iptables rules and first application will be on port 80, next 81, and so on.In browser you have to type then: apps.test.com (first app) , apps.test.com:81 (second app)..
Another solution: if you want to have diffrent domains (not type a port after colon), you can use proxy server,
and use VirtualHost to redirect to particular apps. In proxy configuration you can define that app1.test.com goes to port e.g 8888, app2.test.com goes to port 8889 and then during start up your app you can create iptables rule or ssh tunel to redirect whole traffic from port 8888 to your random port of
ruby application. To don't do it more complicated, it would be nice that these port
which you configure in proxy, are not used by your ruby application. Also you can check, iptables
"string match" option; Match Host field of HTTP request and then analogously during start up apps,
create iptables rule which will redirect everything which goes to port 80 with specify Host field to
your ruby app port. The last option would be using SRV dns record, but it's rather useless in your
situation. But anyway you can play around with all of these options, and choose one which is the best for
you.

Related

Make a subdomain to point on a specific port

I already checked some topic about it but didn't find any solutions (if it's possible). I have a domain that points on my server on the port 80, but, I have another important webservice running on the port 8080.
I want to know if it's possible to create a subdomain like (admin.example.com) which points on port 8080.
Thanks
The simple answer is no. The server name is resolved by a DNS query to a single IP, to which port the connection is made is between the application and the server. For HTTP the conventional default port is 80 and HTTPS 443, if you need to use another port, you need to include it in your URL.
SRV entries in a DNS record can be used so resolve a hostname to a specific port, but this works reliably only for a handful of protocols that mandate its use.
Currently the preferable way is to set up your server with a reverse proxy to direct traffic by a specific server name (your subdomain, carried in the request headers) to your admin service. This is quite easily done using e.g. nginx.

Hosting Nodejs application without port

I have a nodejs application running on port 3000. I wanted to host it on Linux environment. So I installed nodejs in it. It's working fine but I should specify the port each time.
example: mydomain.net:3000/url_i_want,
How can I avoid this. and also when running my app like that, all users are kind of connected to each others. If one of them disconnect all other users are. If one of them change page all others have there pages changing. Is it because they are all listening to the same port 3000 ? I searched and found that it can be related to PM2 and Nginx. Is it the solution ?
Whenever you load a URL without specifying the port number, the browser defaults to 80, because 80 is the default port number for HTTP.
So if you load http://stackoverflow.com/questions, the browser "converts" it to http://stackoverflow.com:80/questions.
If you don't want a port number to be specified to access your website, your app should be listening on port 80, instead of 3000.
However, it is not recommended for Node apps to directly listen on port 80 (although they very well can).
What you can do is use a front-facing proxy such as nginx, which accepts connections to the host's port 80, and then redirects the request to localhost:3000, where your app is listening.
It is best to ask one question at a time.
As for your second question, unless you are using some sort of "remote syncing" framework, that sort of behavior is unexpected. I would suggest posting a separate question for that issue with more details about it.

How can I set a specific application to use a different port in IIS7?

I want to use a port other that port 80 for a specific application. (It's the speedtest.net mini app: http://www.speedtest.net/mini.php).
I want to use a port other that 80 for it, as we have a WAN optimisation tool on the network. As such, in order to get a "true" test (without the traffic having been cached or compressed) I need to add an exclusion rule in the WAN optimisation too for every host I want to run the test on. (If I stop optimising http traffic for all hosts it would defeat the point of the WAN optimisation tool!)
It would be much easier if I could run the test from some arbitrary port as I could then do a rule for all hosts on this port.
I can see how to edit the bindings for the Default Web Site, but not for a single application. (Or if I set another port up would this ripple to all applications, but wouldn't matter as they'd still be accessible on Port 80, too.)
End result is that I want to get to all my sites on port 80 except one:
http://myserver/speedtest:8765 (or some other port)
You have to go in Control Panel -> Windows Firewall -> Advace Settings -> and then Connecting Rules ( or something like that).
And it will load a list of all your program where you can change port and everything.

significance of node.js port choice

I am a bit new to server side scipts. I am fairly capable with javascript so I decided to take a look at node.js as opposed to php or python ect. Correct me if I am wrong but it seems that when I code my webserver I may freely choose the port number I listen to. Is there any significance to this port number or may I choose any random number I wish? Will anyone be able to send a request to my server regardless of the number I choose?
Thanks in advance!
If you want to run node.js directly without any supporting web server or reverse proxy (no nginx, varnish, apache, etc), you need to listen on port 80 for HTTP and (optionally) 443 for HTTPS if you want normal URLs to work. Otherwise users will need to type the port number in the URL like http://example.com:3000 which is unheard of for public-facing sites.
However, you almost certain DO want to use a separate web server as I describe in detail here, in which case any port over 1024 is fine.
If you have root access you can choose any port that's not already bound to a process.
If you do not have root access you can choose any port above 1024 that is not already bound to a process.
Port 80 is usually the one you want to use if you're serving up HTTP, however, you can access an HTTP server on any port via the URL port syntax. For example, a server on port 3000. http://yourdomain.com:3000
If you're running on Linux and you do not want to run your Node process as root, you can redirect port 80 traffic to another port.

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