Net Core 6 Systemd 服务如何使用 SSL

问题描述 投票:0回答:1

我有一个在Linux上作为systemd服务运行的net core 6 api

这是配置文件

[Unit]
Description=Van Jugando NETCore API

[Service]
WorkingDirectory=/var/www/vanjugando
ExecStart=/usr/bin/dotnet /var/www/vanjugando/VanJugando.dll
Restart=always
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=ASPNETCORE_URLS=http://0.0.0.0:3000
SyslogIdentifier=VanJugando

[Install]
WantedBy=multi-user.target

我还有一个在 Apache2 上的 /var/www/html 端口 :80 上运行的 Web 应用程序,该应用程序具有 SSL 功能(使用 certbot SSL 生成), 我也想在端口 3000 上为我的 net core 6 api 实现 SSL,但不知道如何使用 systemd 服务来做到这一点

问题是如何在我的 .service 文件上配置 SSL?

linux ssl .net-core systemd certbot
1个回答
0
投票

所有语言都有加载私有文件和证书文件所需的工具,以通过 https 公开服务。

nodejs

var https = require('https');
var options = {
    key: fs.readFileSync('example.key'),
    cert: fs.readFileSync('example.crt')
};
https.createServer(options, my_app).listen(3000);

java(春季启动)

server:
  ssl:
    key-store: classpath:keystore.p12
    key-store-password: password
    key-store-type: pkcs12
    key-alias: springboot
    key-password: password
  port: 8443

c#

查看一些帖子,c# 需要 pfx 文件。要从您的证书和私钥创建它,请使用 this:

openssl pkcs12 -export -out domain.name.pfx -inkey domain.name.key -in domain.name.crt

之后,您可以在Program.cs中设置它来加载pfx文件

var httpsConnectionAdapterOptions = new HttpsConnectionAdapterOptions
{
    SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
    ClientCertificateMode = ClientCertificateMode.AllowCertificate,
    ServerCertificate = new X509Certificate2("./certificate.pfx", "password")
 
};
builder.WebHost.ConfigureKestrel(options =>;
    options.ConfigureEndpointDefaults(listenOptions =>;
        listenOptions.UseHttps(httpsConnectionAdapterOptions)));

建议

正如我所解释的here,建议不要直接在源代码应用程序级别处理https。

最明显的缺点之一是每个开发人员都需要证书才能在其本地主机中启动应用程序:/

您应该在另一层处理 https,例如 nginx、apache、haproxy、aws loadbalancer 等。这样您的目标应用程序将是 http,但公开为 https

在您的情况下,如果您有 apache 并且您的应用程序在 3000 端口启动,您可以在某些文件中添加此虚拟主机,例如 /etc/apache2/sites-available/sample.conf

<VirtualHost *:80>
ServerName www.sample.com

SSLEngine on
SSLCertificateFile /etc/ssl/ssldragon_com.crt
SSLCertificateKeyFile /etc/ssl/ssldragon.key
SSLCertificateChainFile /etc/ssl/ssldragon_com.ca-bundle

ProxyPreserveHost On
ProxyPass / http:// 127.0.0.1:5000/
ProxyPassReverse / http:// 127.0.0.1:5000/
ErrorLog ${APACHE_LOG_DIR}/sample-error.log
</VirtualHost>

现在,使用以下命令启用新站点配置:

sudo a2ensite sample.conf

如果没有错误,那么您的网站就可以使用 https 了!

更多详细信息请参见:https://www.syncfusion.com/blogs/post/hosting-multiple-asp-net-core-apps-in-ubuntu-linux-server-using-apache.aspx

参考文献

© www.soinside.com 2019 - 2024. All rights reserved.