11
votes

I have been using ngrok with ASP.NET 4.X without encountering any problems.

Unfortunately, when I try to forward app build in ASP.NET Core 2 I run into a problem that I can't solve.

I tried following combinations of commands to start ngrok:

ngrok http 44374-host-header="localhost:44374"

ngrok http -host-header=rewrite localhost:44374

ngrok http 44374

All give the same result. Ngrok tunnel starts, but when I try to open given forwarding url, site is loading few minutes and then 502 Bad Gateway error is shown. It applies to both http and https version.

Running Visual Studio or ngrok as administrator does not help.

Website works correctly with localhost

Running website with ngrok gives 502 Bad Gateway error

4
it is --host-header not -host-header, that's probably your problemGustavo Topete
Both works the same. I don't think that problem lies in this. Everything works fine on asp.net 4.x with the one I use. – Deleroy 10 mins agoDeleroy
Really?, it didn't work for me when I used just one dashGustavo Topete

4 Answers

12
votes

I solved my problem.

properties/launchSettings.json content:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59889/",
      "sslPort": 44374
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "https://localhost:44374/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "NgrokTEST": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:59890/"
    }
  }
}

So, it turns out that ASP.NET Core uses different port for SLL connection and it's used by default.

Changing port to normal (59890 in my case) in ngrok solved the problem.

6
votes

I want to publish the following fix that may help if somebody is running an ASP Core 2.X app over https with Docker, the following worked for me:

ngrok http https://localhost:[PORT] --host-header="localhost:[PORT]" --subdomain [YOUR_SUBDOMAIN]

Example:
ngrok http https://localhost:44390 --host-header="localhost:44390" --subdomain 2gtest

With that I was able to run ngrok without getting 502 errors.

3
votes

Just go Right click on project->Properties and than Disable SSL.

0
votes

I was able to get ngrok working with http and https by forwarding to the http port. So for example if your app runs on port 5000 for http and 5001 for https, run:

ngrok http 5000