我有一个 .Net Core 2.2 API,它已经过测试,从 Linux 命令 shell(Ubuntu 16.04)启动时可以正常工作,但作为服务运行时,只绑定 http,而不是 https。我知道这更可能是因为需要告知服务环境有关 SSL 的信息,但我没有找到有关如何在服务单元文件中进行设置的任何信息。任何帮助都将不胜感激。
服务单元文件:
[Unit]
Description=Authentication .NET Web API App running on CentOS 7
[Service]
WorkingDirectory=/var/www/Authentication
ExecStart=/usr/share/dotnet/dotnet /var/www/Authentication/Authentication.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-authentication
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
控制台输出:
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
Hosting environment: Production
Content root path: /var/www/Authentication
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
systemctl状态:
● Authentication.service - Authentication .NET Web API App running on CentOS 7
Loaded: loaded (/etc/systemd/system/Authentication.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-07-22 06:48:36 CDT; 38min ago
Main PID: 11506 (dotnet)
CGroup: /system.slice/Authentication.service
└─11506 /usr/share/dotnet/dotnet /var/www/Authentication/Authentication.dll
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: No XML encryptor configured. Key {bc217aa5-1a54-4033-bcb3-0d1bd39cf425} may be persisted to storage in unencrypted form.
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.Server.Kestrel[0]
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Hosting environment: Production
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Content root path: /var/www/Authentication
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Now listening on: http://localhost:5000
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Application started. Press Ctrl+C to shut down.
journalctl 输出:
Jul 22 06:48:36 Chris-PC systemd[1]: Started Authentication .NET Web API App running on CentOS 7.
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: : Microsoft.AspNetCore.DataProtection.Repositories.EphemeralXmlRepository[50]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: Using an in-memory repository. Keys will not be persisted to storage.
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
Jul 22 06:48:38 Chris-PC dotnet-authentication[11506]: No XML encryptor configured. Key {bc217aa5-1a54-4033-bcb3-0d1bd39cf425} may be persisted to storage in unencrypted form.
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: warn: Microsoft.AspNetCore.Server.Kestrel[0]
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Hosting environment: Production
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Content root path: /var/www/Authentication
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Now listening on: http://localhost:5000
Jul 22 06:48:40 Chris-PC dotnet-authentication[11506]: Application started. Press Ctrl+C to shut down.
答案1
发现问题与 www-data 用户 ID 有关。如果我编辑服务单元以使用我的 ID(id 在命令 shell 下运行),则 http 和 https 都会绑定。需要弄清楚与 ww-data 有什么不同。
systemctl状态:
● Authentication.service - Authentication .NET Web API App running on CentOS 7
Loaded: loaded (/etc/systemd/system/Authentication.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-07-22 08:17:05 CDT; 8min ago
Main PID: 19440 (dotnet)
CGroup: /system.slice/Authentication.service
└─19440 /usr/share/dotnet/dotnet /var/www/Authentication/Authentication.dll
Jul 22 08:17:05 Chris-PC systemd[1]: Started Authentication .NET Web API App running on CentOS 7.
Jul 22 08:17:10 Chris-PC dotnet-authentication[19440]: warn: Microsoft.AspNetCore.Server.Kestrel[0]
Jul 22 08:17:10 Chris-PC dotnet-authentication[19440]: Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: warn: Microsoft.AspNetCore.Server.Kestrel[0]
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Hosting environment: Production
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Content root path: /var/www/Authentication
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Now listening on: http://localhost:5000
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Now listening on: https://localhost:5001
Jul 22 08:17:11 Chris-PC dotnet-authentication[19440]: Application started. Press Ctrl+C to shut down.