Possible Duplicate:
How to use DNS to redirect domain to specific port on my server
I want to trick my browser into going to localhost:3000 instead of xyz.com. I went into /etc/hosts on OS X 10.5 and added the following entry:
127.0.0.1:3000 xyz.com
That does not work but without specifying the port the trick works. Is there a way to do this specifying the port?
No, the hosts file is simply a way to statically resolve names when no DNS server is present.
The hosts file is for DNS resolution. DNS resolve names to IP addresses, and has nothing to do with ports I am afraid. You will need to use something else in conjunction with the hosts file to redirect the port (Mangle the TCP header by altering the destination port).
With iptables:
Does MAC OS use iptables / netfilter (I didn't think it did)..? If OS X uses iptables you could point xyz.com to some ip in the hosts file like 157.166.226.25 and then:
:-)
You don't need to specify a port in the hosts file. just make the entry like you did omitting the port, as in 127.0.0.1 xyz.com, this will direct you to your local host, then simply add port 3000 to the end of your URL... http://xyz.com:3000
Assuming you're trying to intercept http and not https, you'd have to be listening on port 80 on your local machine, but then you might be able to use ssh's port forwarding features by ssh'ing to localhost with
-L80:localhost:3000
, but you'll have to do that as root.Would probably be better to have whatever it is that is running on port 3000 just listen to port 80 instead.
If you control the router between you and xyz.com, you might be able to setup a port forwarding rule instead.
The DNS solution for this is to use SRV records:
http://en.wikipedia.org/wiki/SRV_record
These are a way to allow DNS, which was originally a "name to number" or "number to name" distributed database to include "name to service endpoint", which could (optionally) include a protocol and port.
The bad news is that applications have to be developed to use SRV records, so it's not a drop-in solution for what you're trying to do.
I think you need to use some kind of proxy server or maybe something with firewall software to redirect port connections...
Chief-AG is right in that the hosts file is used to statically resolve names (DNS presence is irrelevant). However, there may be a combination of things you could do.
Seems to be a fair bit of work, but it would accomplish what you're asking.
As an alternative to ;'s virtual hosts, you could create an ssh tunnel listening on port 80 and forwarding to localhost:3000.
i'm assuming this is for rails development? if so, then run script/server -p 80 to make it run on the standard web port. then your xyz.com will work
If you are using Apache as a webserver on xyz.com, you could use Apache's
ProxyPass
to 'convert' to to a different port.