简体   繁体   中英

ASP.NET Core 2.0 ngrok 502 Bad Gateway Error

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

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.

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.

只需右键单击项目->属性,然后单击禁用SSL

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

This is a problem with ngrok. For ngrok to work with .Net Core, you have to..

  • Sign up for an ngrok account
  • ngrok will give you an authtoken when you log in the next time. Copy it to notepad.
  • at the command prompt, type ngrok authtoken . You should get a message that a .yml file has been created for you.
  • at the same command prompt, type ngrok http https://localhost:44358 -host-header="localhost:44358" ...or whatever port VS gave you.
  • ngrok will respond with a regular url and an https url (I always take the https one)

If you're writing your API in Java, it's far simpler. All you have to do is type ngrok http .

Not sure why ngrok likes Java so much better than .Net, but that's the world we live in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM