-
创建WebApi模板并发布
-
添加中间件
//app.UseHttpsRedirection();//停用https重定向 using Microsoft.AspNetCore.HttpOverrides; app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); app.UseAuthentication();
-
配置Nginx
server { listen 81; server_name example.com *.example.com; location /api/ { proxy_pass http://127.0.0.1:5000/api/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; 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; } }
-
添加守护进程
//新建文件 vim /etc/systemd/system/kestrel-myapi.service //编辑 [Unit] Description=Example .NET Web API App running on CentOS [Service] WorkingDirectory=/root/public/linux-x64 ExecStart=/usr/bin/dotnet /root/public/linux-x64/MyFirstDotnetAPI.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=root Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target //wq //systemctl start kestrel-myapi.service //systemctl status kestrel-myapi.service //systemctl enable kestrel-myapi.service
-
关闭SeLinux
vim /etc/selinux/config // SELINUX=disabled //wq init 6
如果不使用反向代理,可绑定所有本机IP远程访问
builder.Services.AddControllers();
builder.WebHost.UseUrls(new[] { "http://*:5000" });