我正在尝试使用 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.
发布asp.net core应用程序时,需要注意两个参数:内容根和Web根。
Content root是后端需要的文件所在的文件夹,通常是configs。内容根目录默认为当前目录,也称为工作目录。
Web 根目录是包含通过 HTTP 分发的静态文件的文件夹。默认 Web 根目录是内容根目录内的 wwwroot 文件夹。
因此,当您规定工作目录时,您必须指定它而不是“胡说八道”,但您必须指定您的
appsettings.json和wwwroot.这里有两种方法。如果您在调试时犯了错误,那么只需重复通常从 IDE 启动应用程序的配置即可:
但是,为了在生产中使用,您需要使用以下命令准备应用程序
dotnet publish并将结果复制到永久位置:另外,我记录一下 nginx。如果你使用的是 nginx,那么为什么要依赖你的应用程序来分配静态数据,为什么不把这件事委托给 nginx 呢?
它看起来像这样: