Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using port number in Windows host file

Tags:

windows

After installing TeamViewer, I have changed the wampserver port to 8080, so the address is http://localhost:8080.

For the host file located at C:\WINDOWS\system32\drivers\etc\, I have also made the change as below

BEFORE
127.0.0.1 www.example.com

AFTER
127.0.0.1:8080 www.example.com

When I access www.example.com, it doesn't redirect to my wampserver, how can I fix it?

like image 812
Charles Yeung Avatar asked Dec 28 '11 07:12

Charles Yeung


People also ask

What is port number in hostname?

For example, if the hostname is '192.168.1.100' and the port number is 443, your connection URL could be: https://192.168.1.100/login. If the hostname is 'mycompanyname.com' and the port number is 9443, then your URL could be: https://mycompanyname.com:9443/login.

How do I find port number in Windows?

How to find your port number on Windows. Type “Cmd” in the search box. Open Command Prompt. Enter the netstat -a command to see your port numbers.


11 Answers

I managed to achieve this by using Windows included Networking tool netsh.

As Mat points out : The hosts file is for host name resolution only, so a combination of the two did the trick for me.

Example


Overview

example.app:80
 |                           <--Link by Hosts File
 +--> 127.65.43.21:80
       |                     <--Link by netsh Utility
       +--> localhost:8081

Actions

  • Started my server on localhost:8081
  • Added my "local DNS" in the hosts file as a new line
    • 127.65.43.21 example.app
      • Any free address in the network 127.0.0.0/8 (127.x.x.x) can be used.
      • Note: I am assuming 127.65.43.21:80 is not occupied by another service.
      • You can check with netstat -a -n -p TCP | grep "LISTENING"
  • added the following network configuration with netsh command utility
    • netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
  • I can now access the server at http://example.app

Notes:
- These commands/file modifications need to be executed with Admin rights

- netsh portproxy needs ipv6 libraries even only to use v4tov4, typically they will also be included by default, otherwise install them using the following command: netsh interface ipv6 install


You can see the entry you have added with the command:

netsh interface portproxy show v4tov4

You can remove the entry with the following command:

netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.65.43.21


Links to Resources:

  • Using Netsh
  • Netsh commands for Interface IP
  • Netsh commands for Interface Portproxy
  • Windows Port Forwarding Example
like image 178
Pau Coma Ramirez Avatar answered Oct 02 '22 10:10

Pau Coma Ramirez


The hosts file is for host name resolution only (on Windows as well as on Unix-like systems). You cannot put port numbers in there, and there is no way to do what you want with generic OS-level configuration - the browser is what selects the port to choose.

So use bookmarks or something like that.
(Some firewall/routing software might allow outbound port redirection, but that doesn't really sound like an appealing option for this.)

like image 36
Mat Avatar answered Oct 04 '22 10:10

Mat


What you want can be achieved by modifying the hosts file through Fiddler 2 application.

Follow these steps:

  1. Install Fiddler2

  2. Navigate to Fiddler2 menu:- Tools > HOSTS.. (Click to select)

  3. Add a line like this:-

    localhost:8080 www.mydomainname.com

  4. Save the file & then checkout www.mydomainname.com in browser.

like image 38
Rajat Gupta Avatar answered Oct 05 '22 10:10

Rajat Gupta


Fiddler2 -> Rules -> Custom Rules

then find function OnBeforeRequest on put in the next script at the end:

if (oSession.HostnameIs("mysite.com")){
    oSession.host="localhost:39901";
    oSession.hostname="mysite.com";
}
like image 44
Sergei Avatar answered Oct 04 '22 10:10

Sergei


The simplest way is using Ergo as your reverse proxy: https://github.com/cristianoliveira/ergo

You set your services and its IP:PORT and ergo routes it for you :).

You can achieve the same using nginx or apache but you will need to configure them.

like image 28
Cristian Oliveira Avatar answered Oct 01 '22 10:10

Cristian Oliveira


This doesn't give the requested result exactly, however, for what I was doing, I was not fussed with adding the port into the URL within a browser.

I added the domain name to the hosts file

127.0.0.1      example.com

Ran my HTTP server from the domain name on port 8080

php -S example.com:8080

Then accessed the website through port 8080

http://example.com:8080

Just wanted to share in case anyone else is in a similar situation.

like image 37
Danbardo Avatar answered Oct 04 '22 10:10

Danbardo


Using netsh with connectaddress=127.0.0.1 did not work for me.

Despite looking everywhere on the internet I could not find the solution which solved this for me, which was to use connectaddress=127.x.x.x (i.e. any 127. ipv4 address, just not 127.0.0.1) as this appears to link back to localhost just the same but without the restriction, so that the loopback works in netsh.

like image 22
aards Avatar answered Oct 02 '22 10:10

aards


  1. Install Redirector
  2. Click Edit redirects -> Create New Redirect

enter image description here

like image 22
Smart Manoj Avatar answered Oct 05 '22 10:10

Smart Manoj


If what is happening is that you have another server running on localhost and you want to give this new server a different local hostname like http://teamviewer/

I think that what you are actually looking for is Virtual Hosts functionality. I use Apache so I do not know how other web daemons support this. Maybe it is called Alias. Here is the Apache documentation:

Apache Virtual Hosts examples

like image 26
DogBot Avatar answered Oct 05 '22 10:10

DogBot


-You can use any free address in the network 127.0.0.0/8 , in my case needed this for python flask and this is what I have done : add this line in the hosts file (you can find it is windows under : C:\Windows\System32\drivers\etc ) :

127.0.0.5 flask.dev
  • Make sure the port is the default port "80" in my case this is what in the python flask: app.run("127.0.0.5","80")

  • now run your code and browse flask.dev

like image 24
Belal Mohammed Avatar answered Oct 04 '22 10:10

Belal Mohammed


You need NGNIX or Apache HTTP server as a proxy server for forwarding http requests to appropriate application -> which listens particular port (or do it with CNAME which provides Hosting company). It is most powerful solution and this is just a really easy way to keep adding new subdomains, or to add new domains automatically when DNS records are pointed at the server.

  • Apache era call it Virtual host -> httpd.apache.org/docs/trunk/vhosts/examples.html

  • NGINX -> Server Block https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

like image 22
Musa Avatar answered Oct 04 '22 10:10

Musa