DotNet(6) Core WebAPI部署到Linux并配置Nginx反向代理




DotNet Core WebAPI部署到Linux并配置Nginx反向代理

  1. 创建WebApi模板并发布

  2. 添加中间件

    //app.UseHttpsRedirection();//停用https重定向
    using Microsoft.AspNetCore.HttpOverrides;
    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
    });
    app.UseAuthentication();
    
  3. 配置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;
        }
    }
    
  4. 添加守护进程

    //新建文件
    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
    
  5. 关闭SeLinux

    vim /etc/selinux/config
    //
        SELINUX=disabled
    //wq
    init 6
    

 

如果不使用反向代理,可绑定所有本机IP远程访问

builder.Services.AddControllers();
builder.WebHost.UseUrls(new[] { "http://*:5000" });

 


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇