• 首页
  • Qt文档
  • DTK文档
  • 玲珑文档
  • UOS通过Nginx托管Net Core服务

    UOS 通过 Nginx 托管 .Net Core 服务

    硬件环境 软件环境 IP 地址
    KVM 虚拟机 UOS 服务器版[X86] 1032 192.168.122.252

    UOS + .Net + Nginx

    一、准备 .Net 环境

    1、解压缩软件包

    # mkdir dotnet
    # tar -zxf dotnet-runtime-3.1.15-linux-x64.tar.gz -C dotnet/
    # tar -zxf dotnet-sdk-3.1.115-linux-x64.tar.gz -C dotnet/
    

    2、优化路径,配置环境变量

    # ln -s $HOME/dotent/dotnet /usr/bin/dotnet
    # vim .bashrc
    export DOTNET_ROOT=$HOME/dotnet
    export PATH=$PATH:$DOTNET_ROOT
    export MASBuildSDKPath=$HOME/dotnet/sdk/3.1.115/Sdks/
    # source .bashrc 
    

    二、编译运行项目

    1、创建一个mvc项目

    # dotnet new mvc -o ntmvc
    The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
    This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore/3.1-third-party-notices for details.
    
    Processing post-creation actions...
    Running 'dotnet restore' on ntmvc/ntmvc.csproj...
      Restore completed in 49.24 ms for /root/ntmvc/ntmvc.csproj.
    

    2、修改 Startup.cs 文件

    ## 首先切换到项目目录ntmvc,修改Startup.cs文件
    # cd ntmvc/
    # vim Startup.cs 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    //添加引用
    using Microsoft.AspNetCore.HttpOverrides;
    ....
    app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
                
                //添加下面的代码
                app.UseForwardedHeaders(new ForwardedHeadersOptions
                {
                    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
                });
    
                app.UseAuthentication();
    

    3、生成项目

    ## 运行下面的命令生成项目
    # dotnet publish -c Release
    Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Restore completed in 41.98 ms for /root/ntmvc/ntmvc.csproj.
      ntmvc -> /root/ntmvc/bin/RElases/netcoreapp3.1/ntmvc.dll
      ntmvc -> /root/ntmvc/bin/RElases/netcoreapp3.1/ntmvc.Views.dll
      ntmvc -> /root/ntmvc/bin/RElases/netcoreapp3.1/publish/
    

    运行完后生成一个bin文件夹,bin 文件夹中会包含 Release 文件夹,在 Release 文件夹中的netcoreapp3.1 文件夹中,会包含可以发布的内容,即publish文件夹。

    4、运行项目

    ## 切换到publish 文件夹,运行命令
    # dotnet ntmvc.dll
    info: Microsoft.Hosting.Lifetime[0]
          Now listening on: http://localhost:5000
    info: Microsoft.Hosting.Lifetime[0]
          Now listening on: https://localhost:5001
    info: Microsoft.Hosting.Lifetime[0]
          Application started. Press Ctrl+C to shut down.
    info: Microsoft.Hosting.Lifetime[0]
          Hosting environment: Production
    info: Microsoft.Hosting.Lifetime[0]
          Content root path: /root/ntmvc/bin/Release/netcoreapp3.1
    

    5、编写成 service 服务,是指开机自启

    # vim /etc/systemd/system/kestrel-ntmvc.service
    [Unit]
    Description=Example .NET Web MVC Application running on UOS
    
    [Service]
    WorkingDirectory=/root/ntmvc
    ExecStart=/usr/bin/dotnet /root/ntmvc/bin/Release/netcoreapp3.1/publish/ntmvc.dll
    Restart=always
    RestartSec=10  # Restart service after 10 seconds if dotnet service crashes
    SyslogIdentifier=dotnet-example
    User=root
    Environment=ASPNETCORE_ENVIRONMENT=Production 
    
    [Install]
    WantedBy=multi-user.target
    

    查看结果

    # systemctl daemon-reload
    # systemctl restart kestrel-ntmvc.service
    # systemctl status kestrel-ntmvc.service
    ● kestrel-ntmvc.service - Example .NET Web MVC Application running on UOS
       Loaded: loaded (/etc/systemd/system/kestrel-ntmvc.service; disabled; vendor preset: enabled)
       Active: active (running) since Thu 2021-05-13 15:36:32 CST; 1s ago
     Main PID: 1622 (dotnet)
        Tasks: 17 (limit: 2313)
       Memory: 30.6M
       CGroup: /system.slice/kestrel-ntmvc.service
               └─1622 /usr/bin/dotnet /root/ntmvc/bin/Release/netcoreapp3.1/publish/ntmvc.dll
    
    5月 13 15:36:32 uos-PC dotnet-example[1622]: info: Microsoft.Hosting.Lifetime[0]
    5月 13 15:36:32 uos-PC dotnet-example[1622]:       Now listening on: http://localhost:5000
    5月 13 15:36:32 uos-PC dotnet-example[1622]: info: Microsoft.Hosting.Lifetime[0]
    5月 13 15:36:32 uos-PC dotnet-example[1622]:       Now listening on: https://localhost:5001
    5月 13 15:36:32 uos-PC dotnet-example[1622]: info: Microsoft.Hosting.Lifetime[0]
    5月 13 15:36:32 uos-PC dotnet-example[1622]:       Application started. Press Ctrl+C to shut down.
    5月 13 15:36:32 uos-PC dotnet-example[1622]: info: Microsoft.Hosting.Lifetime[0]
    5月 13 15:36:32 uos-PC dotnet-example[1622]:       Hosting environment: Production
    5月 13 15:36:32 uos-PC dotnet-example[1622]: info: Microsoft.Hosting.Lifetime[0]
    5月 13 15:36:32 uos-PC dotnet-example[1622]:       Content root path: /root/ntmvc
    

    三、配置 Nginx 托管

    1、安装 Nginx

    # apt install -y nginx
    

    2、配置 Nginx 代理

    # vim /etc/nginx/conf.d/proxy.conf
    server {
            listen  80;
            server_name www.yzq1.com;
    
            location / {
                    proxy_pass https://127.0.0.1:5001;
                    proxy_redirect             off;
                    proxy_set_header         Host             $host;
                    proxy_set_header        X-Real-IP         $remote_addr;
                    proxy_set_header        X-Forwarded-For    $proxy_add_x_forwarded_for;
                    proxy_set_header    X-Forwarded-Proto $scheme;
                    client_max_body_size     10m;
                    client_body_buffer_size 128k;
                    proxy_connect_timeout     90;
                    proxy_send_timeout         90;
                    proxy_read_timeout         90;
                    proxy_buffers            32 4k;
    
            }
    }
    

    4、检查 Nginx 配置并重启

    # nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    # systemctl restart nginx
    

    验证

    需要添加/etc/hosts 解析