Exposing localhost to the internet via tunneling (using ngrok): HTTP error 400: bad request; invalid hostname

asp.netVisual Studio-2012asp.net Web-ApiIis ExpressNgrok

asp.net Problem Overview


From previous versions of the question, there is this: Browse website with ip address rather than localhost, which outlines pretty much what I've done so far...I've got the local IP working. Then I found ngrok, and apparently I don't need to connect via the IP.


What I am trying to do is expose my website running on localhost to the internet. I found a tool that will do this: ngrok.

Running the website in visual studio, the website starts up on localhost/port#. I run the command "ngrok http port#" in the command line. Everything seems to start up fine. I generate a couple of URLs, and the ngrok inspection url (localhost:4040) works.

The only problem is that when I go to the generated URLs, I get an HTTP error 400: bad request invalid hostname. This is a different error than when I run "ngrok http wrongport#", which is a host not found error...so I think something good is happening. I just can't tell what...

Is there a step I am missing in exposing my site to the internet via the tunneling service? If there is, I can't find it in the ngrok documentation.

asp.net Solutions


Solution 1 - asp.net

Troubleshot this issue with ngrok. In the words of inconshrevable, some applications get angry when they see a different host header than expected.

Running the following command should fix the problem:

ngrok http [port] -host-header="localhost:[port]"

Solution 2 - asp.net

Following command will fix the issue

ngrok http -host-header=localhost 8080

Solution 3 - asp.net

This didn't work for me. you could do the following:

For IIS Express

In VS 2015: Go to the .vs\config\applicationhost.config folder in your project

In VS 2013 and earlier: Go to %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config

Find the binding that says:

<binding protocol="http" bindingInformation="*:5219:localhost" />

For me it was a project running on port 5219

change it to

  <binding protocol="http" bindingInformation="*:5219:" />

IIS Express will now accept all incoming connections on that port.

Disadvantage: you need to run IIS Express as admin.

or you could rewrite the host header in Ngrok:

ngrok.exe http -host-header=rewrite localhost:5219

Solution 4 - asp.net

For https this works:

ngrok http https://localhost:<PORT> -host-header="localhost:<PORT>"

Solution 5 - asp.net

The simplest thing for me was using iisexpress-proxy + ngrok.

First I install iisexpress-proxy globally with npm

npm install -g iisexpress-proxy

Then I proxy my localhost with it. Say for instance my site is running on 3003.

iisexpress-proxy 3003 to 12345 where 12345 is the new http port I want to proxy to.

Then I can run ngrok on it.

./ngrok.exe http 12345

It just works! 

But I think it works only with http. Right now I don't use https to test, but even if it works, usually it's a lot of work as always.

Solution 6 - asp.net

Try with different locations from the Global infrastructure > Locations

ngrok http -region eu 8080

You can make a request and view any traffic passing through your tunnel using the ngrok traffic inspector at http://localhost:4040.

OR in command line

ngrok http -region eu 8080 --log=stdout                                                                                                                                                 

If one region fails then try with another.

ngrok runs tunnel servers in datacenters around the world. The location of the datacenter within a given region may change without notice (e.g. the European servers may move from Frankfurt to London).

  • us - United States (Ohio)

  • eu - Europe (Frankfurt)

  • ap - Asia/Pacific (Singapore)

  • au - Australia (Sydney)

  • sa - South America (Sao Paulo)

  • jp - Japan (Tokyo)

  • in - India (Mumbai)

Solution 7 - asp.net

Steps.

  1. Run command on your console from ngrok.exe directory . ngrok http port i.e ngrok http 80 https://www.screencast.com/t/oyuEPlR6Z Set

  2. Ngrok url to your app .

It will create a tunnel to your application.

Thanks .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - asp.netChrisView Answer on Stackoverflow
Solution 2 - asp.netSathishView Answer on Stackoverflow
Solution 3 - asp.netStefanvdsView Answer on Stackoverflow
Solution 4 - asp.netJoel WiklundView Answer on Stackoverflow
Solution 5 - asp.netReuel RibeiroView Answer on Stackoverflow
Solution 6 - asp.netitsazzadView Answer on Stackoverflow
Solution 7 - asp.netRavinder RanaView Answer on Stackoverflow