gpt4 book ai didi

ubuntu - ASP.Net Core 应用服务在 Ubuntu 上只监听 5000 端口

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:42 25 4
gpt4 key购买 nike

我正在尝试在 Ubuntu 服务器上托管一个 ASP.Net Core MVC 应用程序(启用了 https 重定向),使用 Nginx 作为反向代理。我已经使用 OpenSSL 创建并安装了本地 SSL 证书。当我使用 dotnet CLI 运行我的应用程序时,它会同时监听 http://localhost:5000 & https://localhost:5001 ,我可以使用 https 在 Web 上访问它(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

最佳答案

我终于明白了这个问题。问题是开发人员 ssl 证书随 dotnet SDK 一起安装,名称为 localhost。对于 Ubuntu,证书位于/home/{user name}/.dotnet/corefx/cryptography/x509stores/my

Kestrel 只是在执行用户的主目录中搜索,该目录不存在“www-data”,因此无法找到开发证书。因此它不会绑定(bind)到默认的 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

关于ubuntu - ASP.Net Core 应用服务在 Ubuntu 上只监听 5000 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52227372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com