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

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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]"

Method 2

Following command will fix the issue

ngrok http -host-header=localhost 8080

Method 3

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

For IIS Express

In VS 2015:
Go to the .vsconfigapplicationhost.config folder in your project

In VS 2013 and earlier:
Go to %USERPROFILE%My DocumentsIISExpressconfigapplicationhost.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

Method 4

For https this works:

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

Method 5

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.

Method 6

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)

Method 7

For https this works:

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

Method 8

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 .


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x