首页 技术 正文
技术 2022年11月10日
0 收藏 770 点赞 3,395 浏览 1535 个字

1、准备工作:

  a:使用VisualStudioCode创建asp.net core项目,并使用命令“dotnet publish”发布(可以参考前面两篇文章)。

    如:dotnet publish -c release -o D:\coretest 发布到D:\coretest文件夹中,-c 发布时要使用的配置,默认值是debug。

  b:把发布的 asp.net core项目上传到linux服务器上(我的linux的代码路径为“/var/wwwroot/netcoretest”)。

  c:linux服务器上安装.net core sdk。

2、asp.net core代码注意地方:

  由于请求是通过nginx反向代理转接的,因此使用 Microsoft.AspNetCore.HttpOverrides 包中的转接头中间件。

  此中间件使用 X-Forwarded-Proto 标头来更新 Request.Scheme,使重定向 URI 和其他安全策略能够正常工作。

  所以在项目的Startup.cs中做如下修改:

  VisualStudioCode创建的asp.net core项目部署到linux,使用nginx代理

  

3、配置nginx:

server {
listen 8003;
server_name example.com *.example.com;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host:$server_port; #加上端口,这样配置,可以防止重定向时端口丢失问题
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

4、在linux服务器上运行项目:

切换到项目代码目录下(我的linux的代码路径为“/var/wwwroot/netcoretest”),使用命令“dotnet admin.dll”(admin为我的项目名)运行项目,如下图:

VisualStudioCode创建的asp.net core项目部署到linux,使用nginx代理

上面的warn可以忽略。

.net core项目默认监听端口为5000,代码中可以在launchSettings.json中修改。

这时,在浏览器中输入地址http://XXXXX:8003 就可以了,如下图(下面是创建.net core mvc 默认站点界面):

VisualStudioCode创建的asp.net core项目部署到linux,使用nginx代理

5、也可以建一个自定义服务,用于维护.net core项目进程,使项目的开机自动启动:

a、新建自定义服务:vim /etc/systemd/system/mydotnetcore.service

b、服务代码内容如下:

[Unit]
Description=dotnet core demo running on linux[Service]
WorkingDirectory=/var/wwwroot/netcoretest
ExecStart=/usr/bin/dotnet /var/wwwroot/netcoretest/admin.dll
Restart=always
RestartSec=
SyslogIdentifier=dotnet core demo
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production [Install]
WantedBy=multi-user.target

c、启动服务

systemctl start mydotnetcore.service

即可。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903