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


    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中做如下修改:

      

      

    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为我的项目名)运行项目,如下图:

    上面的warn可以忽略。

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

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

    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= 10
    SyslogIdentifier=dotnet core demo
    User=root
    Environment=ASPNETCORE_ENVIRONMENT=Production 
    
    [Install]
    WantedBy=multi-user.target

    c、启动服务

    systemctl start mydotnetcore.service

    即可。

  • 相关阅读:
    python 时间差计算
    NET Framework 4.5新特性 (一) 数据库的连接加密保护。
    某表含有N个字段超精简模糊查询方法
    清空javascript数组数据
    IIS无法连接LocalDb,怎么办?
    jquery 模糊查询对象属性
    解释杨中科随机数为什么会骗人?
    前端Js传递数组至服务器端
    javascript获取客户端默认打印机
    水晶报表注意的问题
  • 原文地址:https://www.cnblogs.com/qk2014/p/10063003.html
Copyright © 2020-2023  润新知