Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to configure the Windows hosts file to send host requests based on IP address + ports.

For example:

127.0.0.1:80       www.site1.com
127.0.0.1:8080     www.sitetwo.com

Because I have Apache listening on port 8080 and IIS on port 80 (default).

So, I think the best way to do this is to modify the hosts file. It's not affecting anything. Neither I am getting any error nor any message.

share|improve this question
1  
You'll get a better response asking this sort of question on serverfault.com – Carl Norum Feb 21 '10 at 18:20

migrated from stackoverflow.com Feb 22 '10 at 5:46

4 Answers

You can't. The hosts file is just that -- hosts. It's the equivalent of a "short-circuited" DNS within your local machine. DNS can't send you to ports, either.

To achieve what you're trying to do, you would want to set up virtual hosting under IIS or Apache, and use that (based on the HTTP Host: header) to let the service decide which site to present. This won't help you, though, if you want to be doing this with both IIS and Apache simultaneously.

share|improve this answer
could you be please clear, i didnt understand about virtual hosting. How can i do the virtual hosting? – SIA Feb 21 '10 at 18:43

No, you can't. If it's not working, then something else is going wrong.

127.0.0.1 site.com
127.0.0.1 www.site.com

I assume this is for a development box?

DNS will resolve site.com and www.site.com to localhost as long as you're not doing something like proxying your DNS queries. You should probably ask over at ServerFault for webserver configuration help, that's the most likely issue.

share|improve this answer
Thanks for the help!!! But there is no way??? Then how can i run iis and apache parallelly with different ports – SIA Feb 21 '10 at 18:41
They're still going to the same address, the port doesn't even factor into what hosts does. The port will still work, you can run them in parallel. – Xorlev Feb 21 '10 at 19:05

As with the other answers host file doesn't have port information but you can have a page that redirects biased upon the header like apache or IIS would do for name based virtual hosting. Check the header and redirect as needed. However I don't really see the value in this as you are reinventing the wheel. Apache supports port based virtual hosting and I assume that iis does.

Is your goal to have a development machine with both running our is this for a production situation.

share|improve this answer

You can do that with Fiddler. With Fiddler script, you can many amazing things.

If you set hosts like this

127.0.0.1     www.site1.com    # Port 80
127.0.0.1     www.sitetwo.com  # Port 8080

Add this in CustomRules.js(to open CustomRules.js, choose Customize Rules on Fiddler's Rules menu)

// this method is already exist
static function OnBeforeRequest(oSession: Session) {  
    if (oSession.host.toLowerCase() == "www.sitetwo.com") 
        oSession.host = "www.sitetwo.com:8080";
    ...
}

Then Fiddler convert the host, and you'll connect to port 8080.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.