作者:默然b并不是我的选择 | 来源:互联网 | 2022-12-05 14:11
我正在尝试使用Nginx作为反向代理在Ubuntu服务器上托管ASP.Net Core MVC应用程序(启用https重定向)。我已经使用OpenSSL创建并安装了本地SSL证书。当我使用dotnet CLI运行应用程序时,它会同时监听http:// localhost:5000和https:// localhost:5001,并且我能够使用https在网络上访问它(Nginx将HTTP请求重定向到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
1> Praveen Rai..:
最后,我弄清楚了这个问题。问题是在dotnet SDK中安装了名为localhost的开发人员ssl证书。如果是Ubuntu,则证书位于/ home / {用户名} /.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/",
"Password": "xyz123"
}
}
}
}
}
然后,您需要将www-data用户添加到ssl-certs组。下面是命令行:
sudo usermod -aG ssl-cert www-data