【问题标题】:Running .NET Core 5.0 on Debian Nginx在 Debian Nginx 上运行 .NET Core 5.0
【发布时间】:2021-06-01 05:35:17
【问题描述】:

我想将我在 Windows 上工作的项目转移到 Unix 机器上运行。

使用 Nginx 在 Debian 9 上运行的机器。

这个项目在带有 IIS 的 Windows 上运行得非常好。

我已按照here 上的所有说明创建了一个服务,以便在机器启动时运行此服务,并使用 Nginx 配置代理从我要使用的端口到端口 5000 的连接。

当我启动运行 Dotnet Myddl.dll 的应用程序时,它会启动并说它只在侦听端口 5000。

然后当我尝试访问它时,我会看到一个警告。

警告:Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] 无法确定重定向的 HTTPS 端口。

我知道这与我的应用重定向到 HTTPS 并且不知道将其重定向到哪里有关,但我该如何解决这个问题?

我的服务

[Unit]
Description=Myapp API

[Service]
WorkingDirectory=/var/www/myapp/publish
ExecStart=/usr/bin/dotnet /var/www/myapp/publish/myapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target





   server {
    listen        6969;
    server_name   mysite.net *.mysite.net;
    location / {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

编辑:

我一直在尝试解决这个问题,但仍然无法解决。当我在 unix 机器上启动应用程序时,我得到以下信息

root@myhost:/var/www/myapp/publish# dotnet Myapp.dll info: Microsoft.Hosting.Lifetime[0] Now listening on: localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: /var/www/myapp/publish 

显然它缺少 https 选项,我不知道为什么。

编辑2: 我已经将应用程序发布为 Linux-x64 的自包含应用程序,现在我没有收到警告说它无法确定 https 端口,在我的浏览器上,当我访问时我被重定向到 https://mydomain:5001它在 http://mydomain:6969 我仍然没有让应用程序在 Unix 上监听 https,只是在 Windows 上。

编辑3: 注意到如果我去我的端点之一,例如http://IP:6969/api/users 我收到 500 响应。

编辑4: 当我在本地加载我的应用程序时,我直接进入了招摇页面,例如 /swagger/index.html,由于某种原因,我的 API 在为 Linux 编译时不接受此 URL 并返回 404,但如果我到达我的一个端点,例如/api/users,它确实返回了我期望的数据。

【问题讨论】:

    标签: linux .net-core debian


    【解决方案1】:

    默认 https 端口应为 5001。

    您可以在官方docs之后设置https端口。

    或禁用relatied middlware。如果需要,使用 nginx 终止 https。

    【讨论】:

    • 感谢您的回复。问题是,如果我在 Windows 上运行它,我有可用的端口 5001,但在 Unix 上我没有。
    【解决方案2】:

    我正在尝试迁移一个 API,并且在 Windows 上它让我返回到位于 /swagger/index.html 上的我的招摇页面,我期待同样的事情会发生,但由于某种原因,它不会。

    因此,如果我访问我的端点之一(例如 /api/users),它确实可以正常工作。

    【讨论】:

      猜你喜欢
      • 2019-11-25
      • 2021-05-25
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多