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

12

297

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.

Superimpose answered 29/5, 2015 at 17:9 Comment(1)
The whole process is summarized here. Maybe this could help.Protium
S
781

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

Depending on the version, you may also want to try:

ngrok http [port] --host-header="localhost:[port]"
Superimpose answered 2/6, 2015 at 16:16 Comment(13)
Thanks. I previously followed the instructions from Devin Rader detailed here, but this now makes this way easier. twilio.com/blog/2014/03/…Philhellene
This solution also works if you are having trouble adding another binding to the applicationHost.config (vs2015/iisexpress). You don't need to add one just use this answer. Finally, if you paid for a custom domain to avoid changing the address every time just add -subdomain=mysubdomain to above answer.Merrymerryandrew
It worked. Just in case of using Social Authentication in asp.net 5 mvc 6, somehow, the return URL is becoming localhost/signin-facebook instead of using my subdomain. Any idea how to fix that?Semipermeable
@Sanju Not sure; it's been a year since I've used NgrokSuperimpose
Can't upvote this enough! For all the complex and buggy instructions out there to hack your IISExpress bindings and network settings, this cuts through it all like a hot knife through butter.Lunik
When local is on https instead of http then this variation of above works: ngrok http https://localhost:44362 -host-header="localhost:44362"Carreno
wow, I wasted almost 2 hours figure this one out. my only problem was I was adding https in -host-header. like this: ngrok http https://localhost:44392 -host-header="https://localhost:44392" removing https:// from -host-header parameter solved my problem. ThanksMauramauralia
This used to work for me, but appears you must have Pro or Enterprise version to get host header rewrite capabilities, at least that is the error that was returned to me todayBillfish
thanks, it still works in 2022. just as --host-header=whateverSample
@Sample Thanks. And yeah: I'd hold off on spending money pre-working prototype and continue to problem solve if you can't get it working. There might be something else wrong.Superimpose
I set it as the subdomain and the terminal looks like it's redirecting fine but when I go to the ngrok link it takes me to the default welcome rails page instead of to my subdomain. Any ideas?Schoonmaker
On my version of ngrok (3.3.0), the command is --host-header="localhost with two hyphensSupplicant
thanks, this work for me, in web aplication with .netSanjay
C
72

Following command will fix the issue

ngrok http -host-header=localhost 8080
Chor answered 7/8, 2017 at 10:42 Comment(3)
If using ASP.NET Core you should also comment out app.UseHttpsRedirection() in your Startup class to avoid a 307 Temporary Redirect.Fledgy
For some reason the accepted answer didn't work for me, this solution did. Thanks!Inexhaustible
didn't work for meTenrec
M
44

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
Meuse answered 24/11, 2015 at 3:2 Comment(1)
This is the correct way to rewrite header in ngrok. None of the other answers worked. Already got IIS set to any hostname, but ngrok returned "hostname invalid" when using -host-header="localhost:8892" however this rewrite version worked immediately.Slavey
C
34

For https this works:

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

Chicanery answered 17/1, 2020 at 14:17 Comment(1)
this definitely worked for me : ngrok http --host-header=MY_DOMAIN.COM 443Governor
P
10

UPDATED COMMAND FOR LATEST VERSION

Tested with: (Windows) (ngrok v3.0.5)

Use -- instead of -

ngrok http --host-header=localhost 8080

Paletot answered 30/6, 2022 at 13:12 Comment(0)
T
2

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.

Toshikotoss answered 9/4, 2019 at 19:22 Comment(0)
F
2

For https this works:

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

Fetter answered 14/6, 2022 at 13:50 Comment(1)
i am triyng to figure how to make it work with the tunnels configuration file ngrok.yml. When i get it, i'll edit this post :)Fetter
S
2

First open ngrok configuration YAML file, run from terminal:

ngrok config edit

Example of yaml for localhost setup (client & server):

version: "2"
authtoken: {YOUR_AUTH_TOKEN_FROM_NGROK_WEBSITE}
tunnels:
 client:
  addr: 3000
  proto: http
  host_header: localhost
 server:
  addr: 4000
  proto: http
  host_header: localhost

Save the config file based on your client and server ports and run the following command:

ngrok start --all

This will make ngrok open a tunnel for all the configurations declared in the yaml file

Shiksa answered 19/6, 2022 at 9:21 Comment(1)
This works, just changed addr to addr: "https://localhost:44329" and host_header to host_header: "localhost:44329" for dotnet core, of course this is just the port mine was serving.Chauvinism
E
0

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)
Enchondroma answered 7/12, 2021 at 5:38 Comment(0)
B
0

Had IIS Express .net web API, had installed NGROK in docker (windows as a host) Had "Bad Request" error, the next command worked for me:

docker run -it -e NGROK_AUTHTOKEN=<token> ngrok/ngrok --host-header=localhost:21852  http host.docker.internal:21852

As I understood later, --host-header needed because IIS Express refuses all requests from outside (must be "localhost:port "), host.docker.internal I've used instead of localhost, because NGROK was running inside docker, while IIS Express was running on a windows host.

Brandtr answered 10/2, 2023 at 14:15 Comment(0)
T
-1

I had the same issue and used the following solution:

  1. Make sure your application binding in your IIS is set to All Unassigned IP address

  2. Run ngrok HTTP 127.0.0.1:173 --region=eu --hostname=yourcustomdomain.eu.ngrok.io

That's it. Works perfectly. This solution is also for paid pro accounts

Thayne answered 12/10, 2022 at 12:1 Comment(1)
Hello, please read Why is it considered inappropriate and unprofessional to type in all capital letters?. Thank you.Butyl
B
-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 .

Baritone answered 11/5, 2018 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.