将 .Net Core 3.1 API 部署到 Linux Centos(Nginx)
Deploying .NetCore3.1 API to LinuxCentos(Nginx)
我正在尝试将 .NET Core 3.1 API + Angular 部署到 Linux Centos it 运行s 在服务器上,但浏览器无法正常打开它。如果我使用 cmd 为 windows 和 运行 发布相同的项目,它 运行 没问题。当我部署它时,我会根据我在 nginx 配置中输入的内容得到这种情况:
localhost:5020/weatherforcast - > I get the API's built in json returning functionality.
localhost:5020/Index.html - > I get index html but only <title> loads and *.js and *.css is not found /so blank white screen with bar title/.
localhost:5020 - > I get 404
我也完全没有路由。
这就是我的启动 Configure 方法的样子:
public无效配置(IApplicationBuilder应用程序,IWebHostEnvironment env)
{
如果(env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
{
context.Response.StatusCode = 200;
context.Request.Path = "/index.html";
await next();
}
});
// if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development) app.UseHttpsRedirection();
app.UseRouting();
if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
{
app.UseCors(builder => builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
}
else
{
app.UseStaticFiles();
app.UseDefaultFiles();
}
app.UseAuthentication();
app.UseAuthorization();
app.UseHsts();
app.UseResponseCompression();
app.UseMiddleware<Middlewares.SeederMiddleware>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<RecievedMessagesHub>("/unread-messages");
endpoints.MapHub<RecipeDetailsTrackHub>("/recipe-details");
});
}
这是我的 Nginx 设置:
server {
listen 80;
server_name app123.tk www.app123.tk;
location / {
proxy_pass https://localhost:5020;
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;
}
}
这就是 Nginx 日志所说的:
[error] 5349#0: *92 connect() failed (111: Connection refused) while connecting to upstream, client: X.X.X.X, server: app123.tk, request: "GET / HTTP/1.1", upstream: "https://127.0.0.1:5020/", host: "www.app123.tk".
如果我 运行 非 angular 应用程序 MVC 在同一端口上使用相同的 Nginx 设置,我就没有这样的问题。
这解决了我的问题:
以下
proxy_set_header X-Forwarded-Proto $scheme;
我加了
proxy_max_temp_file_size 0;
proxy_buffering off;
我正在尝试将 .NET Core 3.1 API + Angular 部署到 Linux Centos it 运行s 在服务器上,但浏览器无法正常打开它。如果我使用 cmd 为 windows 和 运行 发布相同的项目,它 运行 没问题。当我部署它时,我会根据我在 nginx 配置中输入的内容得到这种情况:
localhost:5020/weatherforcast - > I get the API's built in json returning functionality.
localhost:5020/Index.html - > I get index html but only <title> loads and *.js and *.css is not found /so blank white screen with bar title/.
localhost:5020 - > I get 404
我也完全没有路由。 这就是我的启动 Configure 方法的样子: public无效配置(IApplicationBuilder应用程序,IWebHostEnvironment env) { 如果(env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development) { app.UseDeveloperExceptionPage(); }
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
{
context.Response.StatusCode = 200;
context.Request.Path = "/index.html";
await next();
}
});
// if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development) app.UseHttpsRedirection();
app.UseRouting();
if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
{
app.UseCors(builder => builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
}
else
{
app.UseStaticFiles();
app.UseDefaultFiles();
}
app.UseAuthentication();
app.UseAuthorization();
app.UseHsts();
app.UseResponseCompression();
app.UseMiddleware<Middlewares.SeederMiddleware>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<RecievedMessagesHub>("/unread-messages");
endpoints.MapHub<RecipeDetailsTrackHub>("/recipe-details");
});
}
这是我的 Nginx 设置:
server {
listen 80;
server_name app123.tk www.app123.tk;
location / {
proxy_pass https://localhost:5020;
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;
}
}
这就是 Nginx 日志所说的:
[error] 5349#0: *92 connect() failed (111: Connection refused) while connecting to upstream, client: X.X.X.X, server: app123.tk, request: "GET / HTTP/1.1", upstream: "https://127.0.0.1:5020/", host: "www.app123.tk".
如果我 运行 非 angular 应用程序 MVC 在同一端口上使用相同的 Nginx 设置,我就没有这样的问题。
这解决了我的问题: 以下
proxy_set_header X-Forwarded-Proto $scheme;
我加了
proxy_max_temp_file_size 0;
proxy_buffering off;