访问 linux-托管的 ASP.NET 核心 2.x webapp(没有 nginx)

Access linux-hosted ASP.NET Core 2.x webapp (without nginx)

我的 ASP.NET Core 2.1 webapp 在我的开发设置上运行完美。现在我想在生产中测试它。

生产服务器是Ubuntu18,我关注了the instructions。我还不想设置 nginx,只是做一些快速测试,说明上说:

"Either configuration—with or without a reverse proxy server—is a valid and supported hosting configuration for ASP.NET Core 2.0 or later apps".

所以我构建并发布了我的项目:dotnet publish --configuration Release。 然后在服务器上:

它 运行 没有 errors/warnings,并说它正在 http://localhost:5000.

上侦听

在我的本地计算机上,我尝试了 http://serveripaddress(和 http://serveripaddress:5000)但什么也没得到("can't connect")。我可以使用 ssh、sftp 等访问该服务器 - 只有 http 不工作。

我做错了什么?

找到问题了。我需要使用:

sudo ASPNETCORE_URLS="http://0.0.0.0:5000" dotnet MyApp.dll

然后记录 Now listening on: http://0.0.0.0:5000。我可以从远程客户端访问它。

主机默认绑定为 127.0.0.1 ,因此您只能在本地访问该应用程序。如果您想从网络访问它,请添加 --urls 参数。例如:

对于开发,您可以 运行:

dotnet run --urls http://0.0.0.0:5000

对于已部署的项目,您可以 运行:

dotnet MyApp.dll --urls http://0.0.0.0:5000

我使用的dotnet core sdk是2.1.400版本