簡體   English   中英

ASP.Net核心應用程序服務只在Ubuntu上偵聽端口5000

[英]ASP.Net Core application service only listening to Port 5000 on Ubuntu

我試圖在Ubuntu服務器上托管ASP.Net Core MVC應用程序(啟用https重定向),使用Nginx作為反向代理。 我使用OpenSSL創建並安裝了本地SSL證書。 當我使用dotnet CLI運行我的應用程序時,它會同時偵聽http:// localhost:5000https:// localhost:5001 ,並且我可以使用https在Web上訪問它(http請求被Nginx重定向到https) 。

問題是當我嘗試運行作為服務時,它只偵聽http:// localhost:5000

這是* .service文件:

[Unit]
Description=Test ASP.Net core web application service.

[Service]
WorkingDirectory=/home/ubuntu/MyAppFolder
ExecStart=/usr/bin/dotnet/home/ubuntu/MyAppFolder/MyApplication.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=MyApplication
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Development
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=ASPNETCORE_HTTPS_PORT=5001
Environment=ASPNETCORE_URLS=http://localhost:5000;https://localhost:5001

[Install]
WantedBy=multi-user.target

環境細節 :ASP.Net Core 2.1.1,ASP.Net Core SDK 2.1.3,Nginx 1.14,Ubuntu 16.04

最后我想出了這個問題。 問題是開發人員ssl證書與dotnet SDK一起安裝,名稱為localhost。 如果是Ubuntu,證書位於/ home / {user name} /.dotnet/corefx/cryptography/x509stores/my

Kestrel只搜索執行用戶的主目錄,這對於'www-data'是不存在的,因此無法找到開發證書。 由於它沒有綁定到默認的https端口。

為了使其正常工作,我首先使用OpenSSL PEM( .crt)格式的現有證書轉換為PKCS12( .pkf)。 以下是命令。

sudo openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile more.crt

然后我需要使用appsettings.json文件將此證書指定給Kestrel服務器。 下面是該文件現在的樣子:

{
  "ConnectionStrings": {
    "PostgresConnection": "Host=localhost; Database=postgres; Username=postgres; Password=xyz123"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },

  "Kestrel": {
    "Endpoints": {
      "HTTPS": {
        "Url": "https://localhost:5001",
        "Certificate": {
          "Path": "/etc/ssl/certs/<certificate.pfx>",
          "Password": "xyz123"
        }
      }
    }
  }
}

然后,您需要將www-data用戶添加到ssl-certs組。 下面是命令行:

sudo usermod -aG ssl-cert www-data

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM