C# 项目部署到linux服务器nginx转发

阅读 (287)

参照微软官网的linux服务器部署c#所需要的东西装一遍后

c# 发布代码上传到linux服务器后,创建务服

vi /etc/systemd/system/kestrel-helloapp.service

写入配置

[Unit]
Description=Example .NET Web API App running on Linux

[Service]
WorkingDirectory=/var/www/helloapp
ExecStart=/usr/bin/dotnet /var/www/helloapp/helloapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

:wq保存并退出

启动命令与查询状态命令:

systemctl enable kestrel-helloapp.service // 启用
systemctl start kestrel-helloapp.service // 启动
systemctl status kestrel-helloapp.service // 查看进程

nginx 转发

 location / {
                proxy_pass         http://127.0.0.1:5000;
                proxy_http_version 1.1;
                proxy_set_header   Upgrade $http_upgrade;         #这里微软官方文档给的是proxy_set_header  Connection keep-alive, 但使用keep-alive, SignalR会握手失败
                proxy_set_header   Connection "upgrade";
                proxy_set_header   Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
        }

abp发布默认端口为5000

可以在program.cs中增加判断改写配置

// linux使用8000端口
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
                builder.WebHost.UseUrls("http://*:8000");
}

生产环境会调用appsettings.Production.json中的配置项


更新于:2024-04-19 15:58:42
返回顶部