RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1528709
Accepted
Александр Барановский
Александр Барановский
Asked:2023-07-03 20:30:07 +0000 UTC2023-07-03 20:30:07 +0000 UTC 2023-07-03 20:30:07 +0000 UTC

通过 Nginx 在 Ubuntu 20.04 上使用 Angular 发布 ASP.NET Core 应用程序

  • 772

我正在尝试使用 Nginx 在 Ubuntu 20.04 上部署带有 Angular 的 asp.net 应用程序。看起来你需要的一切都已安装、配置,但输入地址时仍然出错。我的配置:

程序.cs

builder.Services.AddControllersWithViews();

/* Подключение контекстов */

var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    //app.UseHsts();
}

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html"); ;

app.Run();

看起来 Nginx 和 ASPNET 之间应该有 HTTP 连接,但用户和 Nginx 可以有 HTTPS。因此,我注释掉了有关 HSTS 和 HTTPSRedirection 的行。

nginx/sites-enabled/domain.ru

server {
    server_name domain.ru www.domain.ru;
    location / {
        proxy_pass         http://127.0.0.1:5000/;
        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;
    }

    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.ru/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.ru/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = domain.ru) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    if ($host = www.domain.ru) {
        return 301 https://$host$request_uri;
    }

    server_name domain.ru www.domain.ru;
    listen        80;
    listen [::]:80;
    return 404;
}

这里我想尽一切办法改变端口。守护进程状态表明应用程序正在端口 5000 上运行,但是请求https://domain.ru将返回 404,如果将端口更改为任何其他端口(5001、7163),则会出现 502 Bad Gateway 错误回。

我使用服务守护应用程序:/etc/systemd/system/daemon.service

[Unit]
Description=Example .NET Web API App running on Linux

[Service]
WorkingDirectory=/home/ASPsite/ASPsite/bin/Debug/net6.0
ExecStart=/usr/bin/dotnet /home/ASPsite/ASPsite/bin/Debug/net6.0/ASPsite.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
#envs
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

有了这一切,如果我在搜索中输入服务器的IP地址,那么Nginx起始页将返回HTTP和HTTPS 404。我已经尝试在ufw 5000和5001中打开端口,它似乎没有帮助。

包.json:

"scripts": {
    "ng": "ng",
    "prestart": "node aspnetcore-https",
    "start": "run-script-os",
    "start:windows": "ng serve --port 44454 --ssl --ssl-cert %APPDATA%\\ASP.NET\\https\\%npm_package_name%.pem --ssl-key %APPDATA%\\ASP.NET\\https\\%npm_package_name%.key",
    "start:default": "ng serve --port 44454 --ssl --ssl-cert $HOME/.aspnet/https/${npm_package_name}.pem --ssl-key $HOME/.aspnet/https/${npm_package_name}.key",
    "build": "ng build",
    "build:ssr": "ng run ASPNETsite:server:dev",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },

点网发布:

MSBuild version 17.3.2+561848881 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  ASPNETsite-> /home/ASPNETsite/ASPNETsite/bin/Debug/net6.0/ASPNETsite.dll

  removed 1 package, and audited 1420 packages in 13s

  181 packages are looking for funding
    run `npm fund` for details

  42 vulnerabilities (2 low, 30 moderate, 10 high)

  To address issues that do not require attention, run:
    npm audit fix

  To address all issues possible (including breaking changes), run:
    npm audit fix --force

  Some issues need review, and may require choosing
  a different dependency.

  Run `npm audit` for details.

  > @fuse/starter@0.0.0 build
  > ng build --prod

  Option "--prod" is deprecated: No need to use this option as this builder defaults to configuration "production".

  <--- Last few GCs --->

  [29962:0x60fe090]   193888 ms: Scavenge 960.4 (1001.7) -> 959.3 (1002.9) MB, 18.7 / 0.0 ms  (average mu = 0.737, current mu = 0.649) allocation failure;
  [29962:0x60fe090]   193968 ms: Scavenge 961.1 (1002.9) -> 960.3 (1004.7) MB, 41.4 / 0.0 ms  (average mu = 0.737, current mu = 0.649) allocation failure;
  [29962:0x60fe090]   194192 ms: Scavenge 963.2 (1004.7) -> 962.1 (1013.4) MB, 179.1 / 0.0 ms  (average mu = 0.737, current mu = 0.649) allocation failure;


  <--- JS stacktrace --->

EXEC : FATAL error : Reached heap limit Allocation failed - JavaScript heap out of memory [/home/ASPNETsite/ASPNETsite/ASPNETsite.csproj]
   1: 0xb7a940 node::Abort() [ng build --prod]
   2: 0xa8e823  [ng build --prod]
   3: 0xd5c990 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [ng build --prod]
   4: 0xd5cd37 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [ng build --prod]
   5: 0xf3a435  [ng build --prod]
   6: 0xf4c91d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [ng build --prod]
   7: 0xf2701e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [ng build --prod]
   8: 0xf283e7 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [ng build --prod]
   9: 0xf095ba v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [ng build --prod]
  10: 0x12ce7ff v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [ng build --prod]
  11: 0x16fb6f9  [ng build --prod]
  Aborted
/home/ASPNETsite/ASPNETsite/ASPNETsite.csproj(58,5): error MSB3073: The command "npm run build -- --prod" exited with code 134.
ubuntu
  • 1 1 个回答
  • 33 Views

1 个回答

  • Voted
  1. Best Answer
    Pavel Mayorov
    2023-07-04T19:16:49Z2023-07-04T19:16:49Z

    发布asp.net core应用程序时,需要注意两个参数:内容根和Web根。

    Content root是后端需要的文件所在的文件夹,通常是configs。内容根目录默认为当前目录,也称为工作目录。

    Web 根目录是包含通过 HTTP 分发的静态文件的文件夹。默认 Web 根目录是内容根目录内的 wwwroot 文件夹。

    因此,当您规定工作目录时,您必须指定它而不是“胡说八道”,但您必须指定您的appsettings.json和wwwroot.

    这里有两种方法。如果您在调试时犯了错误,那么只需重复通常从 IDE 启动应用程序的配置即可:

    WorkingDirectory=/home/my/ASPsite
    ExecStart=/usr/bin/dotnet bin/Debug/net6.0/ASPsite.dll
    

    但是,为了在生产中使用,您需要使用以下命令准备应用程序dotnet publish并将结果复制到永久位置:

    Environment="DOTNET_ENVIRONMENT=Production"
    WorkingDirectory=/opt/ASPsite
    ExecStart=/usr/bin/dotnet ASPsite.dll
    

    另外,我记录一下 nginx。如果你使用的是 nginx,那么为什么要依赖你的应用程序来分配静态数据,为什么不把这件事委托给 nginx 呢?

    它看起来像这样:

        root /opt/ASPsite/wwwroot;
    
        location / {
            try_files $uri index.html; # SPA fallback
        }
    
        location /static {
            try_files $uri;
        }
    
        location /api {
            proxy_pass         http://127.0.0.1:5000/;
            # …
        }
    
    • 1

相关问题

  • ubuntu 中的域名转发

  • 没有规则来构建目标“全部”

  • IP地址设置在应用后几秒钟重置(ubuntu)

  • 如何从 Ubuntu 中删除 Postman?

  • 设置 VPN WireGuard 的问题

  • Fishshell 在加载时写入错误

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5